I have a base class named CollidableObject
and a couple of inherited classes named Player
, Enemy
, InertObject
, etc.
I am trying to find an easy way to iterate through all the instances of them, so I initially created a list of the type CollidableObject
and put all the instances of the inherited classes in there.
The thing is that because of the nature of polymorphism, when I do the following
foreach (CollidableObject CollidableObject in collidableObjects)
{
if (CollidableObject is Player)
{
CollidableObject.Draw(spriteBatch, testPlayerTexture);
}
// Then the same prodedure for each type of CollidableObject.
// Might change to switch or something.
}
The draw method that it calls is the generic Draw method from the CollidbaleObject
base, not the overridden/new one from the Player/Enemy/inertObject class.
How do I get around this. Is there a way to iterate through a collection of objects from the same tree but maintaining their inherited type?
To access an element of a base class from an inherited class, you need to use the base keyword. The base keyword is used in inherited classes in the following cases: when from the constructor of a derived class you need to call the constructor of the base class.
1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.
Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.
You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritance. The order of derivation is relevant only to determine the order of default initialization by constructors and cleanup by destructors.
Is there no way to iterate through a collection of objects from the same tree but maintaining their inherited type?
Absolutely, there is a way to do that. However, you need to set up your hierarchy in a proper way:
Draw
method in the CollidableObject
needs to be marked virtual
Draw
method in the Player
needs to be marked as an override
.This will ensure that the call from your post would be routed to the Player
's override of the method, not to the method of the base.
On a related note, when you see code that checks the dynamic type of an object with the is
operator, as in if (CollidableObject is Player)
, you should get strong suspicions that you are doing something incorrectly. For example, you may be missing a double dispatch.
If all you need to know is the proper texture for the type, you could put textures in a Dictionary<Type,Texture> textureForType
, and pull the right one in your loop using the GetType()
of the collidable object.
Have you tried:
foreach (CollidableObject CollidableObject in collidableObjects)
{
if (CollidableObject is Player)
{
((Player) CollidableObject).Draw(spriteBatch, testPlayerTexture);
}
//Then the same prodedure for each type of CollidableObject. Might change to switch or something.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With