What's an easy way to check type of an object
BlitzMax Forums/BlitzMax Beginners Area/What's an easy way to check type of an object
| ||
I'm using a list of gameobjects which are different types (tplayer, tenemy, tbullet, etc.) If I want to check collision between a bullet and an enemy, I'm doing the following: For Local o:TGameObject = EachIn GameObjects Select o.name Case "Enemy" If ImagesCollide(Self.Image, X, Y, 0, o.Image, o.X, o.Y, 0) Then ListRemove(GameObjects, o) ListRemove(GameObjects, Self) EndIf End Select Next "name" is a field var in my type object. Is there an easier way to find out, what's the current type of object is instead of using select? |
| ||
you can do it like this:For Local o:TEnemy = EachIn GameObjects If ImagesCollide(Self.Image, X, Y, 0, o.Image, o.X, o.Y, 0) Then ListRemove(GameObjects, o) ListRemove(GameObjects, Self) EndIf Next |
| ||
For Local o:TGameObject = EachIn GameObjects Select True Case TEnemy(o) <> Null Print "I am an enemy" ListRemove(GameObjects, o) Case TPlayer(o) <> Null Print "I am a player" 'if you want to call a specific method or field of TPlayer from o Local player:TPlayer = TPlayer(o) player.lives :- 1 End Select Next If speed is a concern, you are better off doing it the way you have done, BUT replacing a string called name that is "Enemy" etc, with an integer TID (type id) field, and declaring a set of constants like Const TPlayerID = 0, TEnemyID = 1 etc as this is faster than checking strings. |