Accessing fields inside a List.

BlitzMax Forums/BlitzMax Programming/Accessing fields inside a List.

ckob(Posted 2006) [#1]
I am trying to Access fields from a Type which I added to a list, after browsing the forums it doesnt look like anyone has asked about this.

Basically I have this type

Entity_List:TList = CreateList()

Type Entity
Field X#,Y#,Z#
Field Model
End Type

Creating the object I just do.

E.Entity = New Entity
E.X# = 10
E.Y# = 10
E.Z# = 10


Now I also have a ListBox which keeps track of the objects created so I can select them however when I select them I would like the XYZ info to be printed to text boxes so how do I extract those fields?


Dreamora(Posted 2006) [#2]
1. You need to add the object to the list
2. When you check through the list, you have to cast the :Object back to Entity by using Entity(someObj) normally. If you iterate over the whole list, there is an easier way thought:

for local ent:Entity = eachin Entity_List
' do something nasty with ent.x / y / z
next


ckob(Posted 2006) [#3]
oops I forgot to add that bit in to the post but I do add it to the Entity_List().

Im not sure I follow what your saying on #2 but the for next loop I dont think would wokr because I am selecting the object from a listbox but I put the object in the listbox according to the number it was created using a simple variable to count. So my list box would look like this

Object: 1
Object: 2
ect..

What I want to do is Click Object: 1 and have the XYZ coordinates show up in 3 seperate text boxes thus the reason I need to access the type entity withen the List.


Fabian.(Posted 2006) [#4]
When you add a gadget item to a listbox you are able to specify an extra object. After the user selected the listbox item you can find the index of the item with SelectedGadgetItem. Now you can get the entity using "Entity(GadgetItemExtra(listbox,index))".


ckob(Posted 2006) [#5]
ok but what exactly would I be doing with the Extra object?

Can someone write a small example so I can see this in action? It doesnt have to work just some kinda psuedo code.


Fabian.(Posted 2006) [#6]
'...
'a list containing all your entities:
Global Entity_List:TList = CreateList ( )

'Your entity type:
Type Entity
  Field X# , Y# , Z#
  '...
EndType
'....
'This function creates a new entity, adds it to the list and to the listbox
Function CreateEntity:Entity()
  Local e:Entity = New Entity
  ListAddLast Entity_List , e
  AddGadgetItem listbox,"Object: "+(EntityList.Count()+1),0,-1,"",e'<-- set e as gadgetitem extra
  Return e
EndFunction
'....
'In this code the events are handled
If EventSource()=listbox And EventID()=EVENT_GADGETACTION
  Local e:TEntity=TEntity(GadgetItemExtra(listbox,SelectedGadgetItem(listbox)))'Get the entity the user double clicked on
  Notify "X: " + e.X + " Y: " + e.Y + " Z: " + e.Z'Notify the entities properties
EndIf



ckob(Posted 2006) [#7]
Awesome got it working thank you.