lists in types
BlitzMax Forums/BlitzMax Programming/lists in types
| ||
Hey all is it poss to have a List as a field in a type? the list would be of another type e.g: type units field uType:string end type type player field unit:tList = CreateList() end type u:units = new units u.uType = "hello" p:player = new player listaddlast p.unit, u would work ok. but if i try to go thru the objects with: For a = EachIn p.units Print p.units.uType Next would give me an error 'foreach index variable must be an object' but i thought it was :) cheers |
| ||
First of all, it's p.unit, not p.units as you coded in your For statement. The variable defined in type player is unit:TList not units:TList. Second of all, since p.unit is a list it returns objects, not integers. So your For statement is wrong because variable a is automatically an integer. You want to say a:units since that is the type that is being returned from the TList p.unit. And then in your loop, you say Print String(a.uType). It would be:Type units Field uType:String EndType Type player Field unit:TList = CreateList() End Type u:units = New units u.uType = "hello" p:player = New player ListAddLast p.unit, u For a:units = EachIn p.unit Print String(a.utype) Next |
| ||
lol, excuse the typo's it was getting late (i notice when ppl make alot of typo's they generally blame time) but i'll give that a blast, the code i have in my .bmx file is free of typo's (i didnt copy and paste the code) thx Rich05 |
| ||
that works great thanks. Only other question is once i've called the listaddlast can i then reuse u? i.e u.uType = "Hello" lisaddlast p.units, u u.uType = "goodbye" lisaddlast p.units, u at the moment i end up with 2 goodbyes if i do this?? or does u have to remain unique for the life of the linked list? |
| ||
You can reuse "u", but not in the way you did it here. What you did here: "u" points to an object of type "units". You set the field "uType" by doing: u.uType = "Hello" Then you add the pointer u to the list, so that a new pointer is added to the list, which points to the same object each time. Then you change the uType field in the object u and add that one to the list too (but it's the same object). Using this code: Type units Field uType:String EndType Type player Field unit:TList = CreateList() End Type p:player = New player u:units = New units u.uType = "hello" ListAddLast p.unit, u u.uType = "Hello" ListAddLast p.unit, u u.uType = "goodbye" ListAddLast p.unit, u For a:units = EachIn p.unit Print String(a.utype) Next This generates only 1 object, while the list has 3 pointers to it. This code prints "Goodbye" 3 times, as there's only 1 object created and you change the field uType a couple of times. If you want to reuse "u" and add 3 objects to the list, each one with it's own setting for the uType field, you can use this code: Type units Field uType:String EndType Type player Field unit:TList = CreateList() End Type p:player = New player u:units = New units u.uType = "hello" ListAddLast p.unit, u u:units = New units ' Create a new object and put it's pointer inside "u" u.uType = "Hello" ListAddLast p.unit, u u:units = New units ' Create a new object and put it's pointer inside "u" u.uType = "goodbye" ListAddLast p.unit, u For a:units = EachIn p.unit Print String(a.utype) Next This code creates 3 objects that are added to the list. Each time the "u" variable holds the pointer to the just created object. So in fact, each time you're calling "u:units = new units", you're overwriting the pointer to the previous object with the pointer to the new object (the previous object isn't lost, because it exists inside the list). |
| ||
ure a diamond, thx |