Basically, what's the most efficient and professional means of checking an object's type when it collides with another object. For instance (ignoring the math for collision detection), suppose we have a gameObject class that game objects inherit from and assume that there is a separate physics system that detects collisions for us, and then proceeds to call the colliding objects collideWithOther method, passing the other object involved in the collision into the method.
abstract class GameObject
{
void abstract collideWithOther(GameObject gameObject);
}
class Bullet : GameObject
{
void override collideWithOther(GameObject gameObject)
{
//Check other object to see if it has game logic related to collision with this object
if (gameObject is Enermy) //this is sudo code, the "is" keyword is to expensive for this.
{
//do bullet hit enermy code here
}
}
}
class Enermy : GameObject
{
...
}
I am aware that there is the "is" keyword and the the .getType meathod. However I have read that they are expensive and sloppy for a real time system such as a game.
To address some of the answers that this question might receive. In the example above we can simply cast the game object to bullet since we know that it would be that class. But the purpose of this question is for a situation where we don't which class it would be. As there may be 10 different classes that can all collide with the bullet, but only 2 of them have game logic associated with it. Also I know we can give the bullet different damaged as an member based on what type of the bullet it is, but that misses the point of the question as this between any object. What would be the most professional and efficient way of dealing with this.
Enums
. AlsoEnums
tend to be more error prone. \$\endgroup\$