Find a unique type in a TList

BlitzMax Forums/BlitzMax Beginners Area/Find a unique type in a TList

Kistjes(Posted 2007) [#1]
Is there a way of finding an unique Type in a list of different types?

I have a global list of TElements containing all kind of child types of TElement (like TPlayer, TEnemy and TDetails). I know there could be one object in that list of type TDetails and I need to verify if that object still exists.

Below you see an example of a 'not so efficient'code. I think there must be a better solution.

Function hasDetails:Byte()
	Local n = 0
	For Local e:TDetails = EachIn gElements
		n:+1
	Next
	Return n>0
End Function



TomToad(Posted 2007) [#2]
Off the top of my head, I can already improve your code with this:
Function hasDetails:Byte()
	Local n = 0
	For Local e:TDetails = EachIn gElements
		Return True
	Next
	Return False
End Function

This way, the loop ends the moment you have a match.


degac(Posted 2007) [#3]
Store in your Type the TLINK to the list. If TLINK<>NULL the item still exists in the list.


Czar Flavius(Posted 2007) [#4]
Do you one to find out if there is at least one of that type, or one and only one?


Kistjes(Posted 2007) [#5]
degac: I don't undrstand your suggestion. Could you give an example?

Czar Flavius: I only need to check if there is an (one) entity of type TDetails. Maybe for an other occasion I want to address the type as well, but then I just need to store the type in a (global) variable.

My question is quite simple: Instead of going through the whole list with a for-next routine isn't there something elegant like:
Print gElements.contains(TDetails)

or something?


TomToad(Posted 2007) [#6]
Nothing that elegant that I know of, but you might be able to implement a reference count to check for. Something like this maybe?



degac(Posted 2007) [#7]
Oh - maybe I have misunderstood what you want..


Kistjes(Posted 2007) [#8]
TomToad: That's interesting. You ask the Type itself if it exists in the list. You do not have to make an instance of it (like you did with TEnemy in the example).
I don't understand the global RefCount. Why is it global and why is the value different within the three types?


Brucey(Posted 2007) [#9]
A way extending TList to track type counts....


Allows you to add any old crap to the list, but will only count TCountable types...


degac(Posted 2007) [#10]
Quite simple.
Every time you create a type (player) and use the AddtoList method, an internal counter will be increased.
While every time you use RemoveFromList the counter will be decreased by 1 unit.
In this way you have to check if tplayer.refcount>0 to know if a tplayer exists or not.


Kistjes(Posted 2007) [#11]
Thank you, everyone.
Very inspiring example codes. Not as simple as gElements.contains(TDetails) ;-) but nevertheless very useful. And, from a programming point of view, nicer than the for-next loop solution.


TomToad(Posted 2007) [#12]
TomToad: That's interesting. You ask the Type itself if it exists in the list. You do not have to make an instance of it (like you did with TEnemy in the example).
I don't understand the global RefCount. Why is it global and why is the value different within the three types?

Globals within a Type are treated differently from Globals in the main program. Type Globals mean that you are applying that value to all instances of that type instead of just one.
So if you add 10 TEnemies to the list, then RefCount will equal 10 in all TEnemies.