I understand that downcasting may not be the best way to achieve my desires, but it at least indicates the kind of solutions I have running through my head. Any alternative suggestions are welcome.
Alright, so may as well dive into my code:
public void triggerItem(Item item)
{
if (item is Weapon) {
equipWeapon(item);
}
}
public void equipWeapon(Weapon weapon)
{
}
So I basically have an inventory set up, and of course the inventory will contain various types of items (weapons, armour, consumables, etc.) As you can see I have an Item class and then a Weapon class and a bunch of others which all inherit Item.
Now, I intend to call this function when the player clicks on an item in their inventory, then call different functions depending on the particular Type of Item.
But because equipWeapon()
receives Weapon, it doesn't like it when I just pass it an Item.
I understand that I could change my code to equipWeapon(Item item)
, but the ultimate destination of the object requires a Weapon type and I'm reluctant to change that. It seems like a dirty solution and a step in the wrong direction, but feel free to correct me if I'm wrong.