For my inventory, I was currently using a TArray of type pickup pointers to act as an inventory, and then for each pickup, they have an integer variable CountOnHold to store the amount I own currently. But then I realized suppose I have multiple pickups, say health potions, even they are the same objects, my array will treat them differently and include them in different index instead of just increasing the count. Is this because these health potions are different pickup pointers? What would be a good way to handle this?
You could derive subclasses from “APickup” for each item type that you want to differentiate.
Say you have:
class AHealthPotion : public APickup
{...}
Then you could cast the APickup to AHealthPotion in your “AddItem” method. If the cast succeeds, you check if you already have a healthPotion in your inventory and then increase the counter on the existing instance in the list.
Otherwise, you add the healthPotion you just picked up to the inventory list.
So say I cast it to “AHealthPotion” and name it health_potion, and do array.contain(health_potion) check. In this case, it actually contain the instance of a health potion instead of the specific one I picked up in game? But the TArray is of type APickup pointer though, would that cause any problem?