DefType?
BlitzMax Forums/BlitzMax Beginners Area/DefType?
| ||
Type MessagesClass Field From:String Field Messages:String Field timer:Int EndType Global MessageBuffer:MessagesClass[12] Function AddToMessages(from:String, messages:String) 'Local clsMessage:MessagesClass = New MessagesClass 'clsMessage.from = from 'clsMessage.messages = messages 'collMessages.AddLast (clsMessage) For Local x:Int = 0 To 8 MessageBuffer[x] = new MessagesClass MessageBuffer[x] = MessageBuffer[x + 1] Next MessageBuffer[9].From = from MessageBuffer[9].Messages = messages End Function This gives errors... not sure if im doing anything wrong? im thinking C++ again |
| ||
copy pasted... no errors here! not sure what the problem is then! |
| ||
MessageBuffer[x] = New MessagesClass MessageBuffer[x] = MessageBuffer[x + 1] Youre New'ing MessageBuffer[x] which is correct, but then setting MessageBuffer[x] to the value of MessageBuffer[x+1] which is going to Null. Don't forget there is a debugger to help out :- http://www.blitzbasic.com/Community/posts.php?topic=95478#1099850 Last edited 2012 |
| ||
@Nate: Add this to the bottom of the code to see the error AddToMessages("Scoobie","Doo") @Neochrome: It appears you are trying to create a FIFO (First In First Out) message system? You may want to look into Linked lists. It looks as though, from the commented out lines, you were trying to use linked lists at one point. To fix your error above, just move the New MessageBuffer line to outside the loop. Note that for the first 8 messages added, part of MessageBuffer[] will be null Last edited 2012 |
| ||
its a more like first in, last out. but THIS has been enough :) Thank you :) still not getting data though, but its working i need it as a data pool a tlist would be ok, but its too messy for the type of stuff i need right now... THANK YOU everyone!! |
| ||
Bit of a new issueType MsgClass Field msgFrom:String Field msgMessage:String Field msgTimer:String End Type Global Msgs:MsgClass[11] For Local BuildI:Int = 0 To 10 Msgs[BuildI] = New MsgClass Next Function AddToMessages(from:String, messages:String) For Local x:Int = 0 To 8 Msgs[x] = Msgs[x + 1] '' THIS doesn't work the way id expect. '' THIS DOES WORK... Msgs[x].msgFrom = Msgs[x + 1].msgFrom Msgs[x].msgMessage = Msgs[x + 1].msgMessage Next Msgs[9].msgFrom = from Msgs[9].msgMessage = messages 'msgFrom[9] = from 'msgMessages[9] = messages 'msgtimer[9] = 0 End Function |
| ||
|
| ||
That's because once you exit the loop, both Msgs[9] and Msgs[8] point to the exact same object. Editing one will edit the other. You need to create a new object for Msgs[9] after exiting the loop. Notice the example I posted before. MessageBuffer[9] = New MessagesClass 'create a new object here. MessageBuffer[9].From = from MessageBuffer[9].Messages = messages |
| ||
Ahh, but 9 is already Created ;) For Local BuildI:Int = 0 To 10 Msgs[BuildI] = New MsgClass Next |
| ||
you can do either this: or this: but I would advise agains the second one, as the second one creates and discards objects while the first one recycles. Last edited 2012 |
| ||
I think you are misunderstanding how objects are stored in Blitzmax. Objects are stored by reference instead of value. Let me see if I can make it clearer. When you create basic variables, a part of memory is reserved to hold the value of that variable. Try this On line 5, the value of a is copied into variable b, just as you would expect. On line 11, the value of b is assigned a new value. Just as you would expect, the value of a does not change as b held only a copy of a before. When you do Local a:int, Blitz will reserve a piece of memory large enough to hold an integer. Likewise, Local b:int reserves a second place in memory for an integer. When you do a = 10, the value 10 is placed in the memory space referenced by a. When you do b = a, the value that was in a is then copied into b. Later when you do b = 15, only the memory space referenced by b changes Now try that with objects instead. Execute this code Now you see that when the value is changed in b, it also changes in a. The reason why is that b does not contain a copy of the object, but rather a copy of the reference. When you do Local a:TType, BM reserves a bit of memory for a reference to an object, not for the object itself. With = New TType, BM will reserve memory for the object itself, assign a reference value to the object, and then place that reference into a. When you try to assign a value to the object with a.AnInt = 10, the object is dereferenced, basically, BM grabs the reference from a, uses that reference to find where in memory the actual object is located. When you do b = a, you copy the reference that BMax created into b. It is the same reference which points to the exact same object. So to illustrate (hopefully) First example code b = a a --Points to--> 10 b --Points to--> 10 b = 15 a --Points to-->10 b --Points to-->15 Second example code b = a a --Points to--> Ref1 --Points to--> TType.AnInt --Points to--> 10 b --Points to--> Ref1 --Points to------^^^^^ b.AnInt = 15 a --Points to--> Ref1 --Points to--> TType.AnInt --Points to--> 15 b --Points to--> Ref1 --Points to------^^^^ So to summarize what I'm saying, During your loop, when x = 8, the reference held in msgs[9] is copied into msgs[8]. The reference is equal, so they both point to the exact same object. That means, any changes made to msgs[9] will also happen to msgs[8]. You will need to create a new msgs[9] completely independent of msgs[8]. You can do it the way Jesse does it in his first example. When you access the fields of an object, the object is already being dereferenced to its value. That's why you do not need to create a new object. As far as to which versions of Jesse's example is better, it depends on how many fields you would need to copy, vs how many times the function would be called. If this is suppose to be a message system in a chatroom, the amount of time copying the fields will outweigh any amount of time spent creating new objects. If it is for processing objects in a multiplayer game, which could potentially be called 1000's of times in a single game loop, then copying each and every field could be the most efficient way. |