Get an Object from Linked List

BlitzMax Forums/BlitzMax Programming/Get an Object from Linked List

FantomKite(Posted 2009) [#1]
Hi to all. I am usually using a TList type to store all my game objects. It is very easy to update or draw them in main cycle. But when i need to get a current one of them, i run a loop, and check every linked object for some special field -- an ID for example. I suggest that there is some another and better way to get an object from list, without wasting precious time. Any advices?
Thanks in advance!


GfK(Posted 2009) [#2]
TMaps.


Warpy(Posted 2009) [#3]
For example:

Type thing
	Field id$
	Field age
	
	Function Create:thing(id$)
		t:thing=New thing
		t.id=id
		thingids.insert id,t	'insert the thing into the map under the key matching its id.
		
		t.age=Rand(10,50)	'make up a random age
		Print t.id+": "+t.age
		Return t
	End Function
	
	Function withid:thing(id$)	'find the thing corresponding to the given id.
		If thingids.contains(id)
			Return thing(thingids.valueforkey(id))
		EndIf
	End Function
End Type

Global thingids:tmap=New tmap

thing.Create("Jim")
thing.Create("Bob")


Print "thing.withid(~qJim~q).age : "+thing.withid("Jim").age


And you can maintain a TList and a TMap at the same time, by the way, and use whichever suits your needs in different parts of the program.


Grey Alien(Posted 2009) [#4]
I concur.

To be more specific I used to used TLists for certain things where I wanted to look something up and then when I found out how TMaps really work I changed to using them.


FantomKite(Posted 2009) [#5]
Thanks a lot!! Can you imagine -- i even didn't knew anything about this useful TMap type.. Thanks again! I'm so happy :D