how to check an objects type
BlitzMax Forums/BlitzMax Beginners Area/how to check an objects type
| ||
hello guys. im trying to write a simple program that draws a laser from a turret object that can be blocked off as it hits a wall or another turret object like a laser pointer. the trouble comes in when i use a for loop to cycle through all the entities currently in existance within the turrets methods. is there any way i can check for an instance's type while in a for loop without using a field? like for i:entity=eachin entitylist if(i.type=turret) check for laser collision(x,y,i.x,i.y) |
| ||
nm! without a field :( Last edited 2011 |
| ||
is there no way to check without using a field? because what im doing seems to not return the right variables. it returns 0 if i dont cast it first but i want to avoid it because i dont want to cast a wall as a turret or the other way around. but i cant use a field because i wont be able to see it before casting it Last edited 2011 |
| ||
wait nvm i figured it out. i stored all the games objects in an entitylist under the type entity. but in order to read its fields i have to cast it to the correct type. but in order to read its type field i have to cast it first which was the problem i was having. but i found a way around it by using a method to return the type field i want. methods work regardless, even if its a turret casted as an entity its methods will still work as long as the parent type has the same method. Last edited 2011 |
| ||
You should make turret a type which extends entity, and then you can iterate for type turret. This will skip non-turrets automatically. |
| ||
OOH so instead of iterating for entity just iterate for turret and it skips non turrets. i thought it converted non turrets into turrets. thanks! =D Last edited 2011 |
| ||
You can try casting an object to a type. If that fails then it's not an object of that type, e.g.:If MyType(obj) Then Print "obj is of MyType" End If |
| ||
:O OH so thats how you check for type thank you so much :D ive been trying to figure that out forever |
| ||
You can of course use Reflection, but that's too slow. |
| ||
In general you should avoid casting to check the type of an object, it can mean bad design. Use methods to get different behavior from different types. |