List and Class

Monkey Forums/Monkey Programming/List and Class

tagoror(Posted 2011) [#1]
Hello,

In Blitzmax we can write an estructure like this:

Type Tobj

field x : float
field y : float

...

End Type


... and to create derived types:

Type Tplayer extends Tobj

...

End Type


Type Tenemy extends Tobj

...

End Type

Suppose we have added all the objects created to a list called myList.
BlitzMax access all the objects added to the list in this way:


For o : Tobj = Eachin myList

.....

Next



If you want to access Tplayer objects we can wrote:

For o : Tplayer = Eachin myList

....

And if we access the Tenemy objects we can wrote:

For o : Tenemy = Eachin myList

----------------------------------------------------

In Monkey, we create the base class:


Class Tobj

....

End ' Tobj


And after that, the derived classes:


Class Tplayer extends Tobj

....

End ' Tplayer


Class Tenemy extends Tobj

.....

End ' Tenemy

To access to all Tenemy and Tplayer objects ....

Local myList := New List <Tobj>

For Local o := Eachin myList

o.render()

Next


... but, here is my question. How can I access from Monkey
to all Tenemy objects only?

For local o ... ?


Thank and kind regards,

Tagoror


GW_(Posted 2011) [#2]
You can promote the object inside the loop.
For Local o ...
	If tEnemy(o) Then
	  'Its of tEnemy
	Else
	  ' its not
	Endif
Next


I'm not sure if all the target languages support the type of casting that BMax eachin does.


ziggy(Posted 2011) [#3]
All target languages, but take into account that Arrays ore Strings are not objects in Monkey.


tagoror(Posted 2011) [#4]
Thank you for the information. I'm going to test it.