I cannot wrap my head around something so seemingly trivial. I have an inventory system that already has a physical and UMG-oriented add/drop/move system (think any standard slot-based RPG inventory). I have a custom class called ItemBase that is used throughout this system. Everything works great - except for when I get to implementing different items. Basically, ItemBase holds a struct ItemInfo for standard variables that all items will have such as Value, IsStackable, Name, etc. I would assume to make a weapon class I would make a child of ItemBase, call it WeaponBase, then add a WeaponInfo struct to this class. My problem arises when I want to add functionality to the child class. My WeaponInfo struct would hold weapon-only info such as Damage, Attack Speed, etc. I’d like to display this info on a tooltip UMG, have such info affect the character’s stats, and so on. However, all of my functionality only works with ItemBase. It cannot work with WeaponInfo since WeaponInfo doesn’t exist in ItemBase. What am I doing wrong? Should ItemBase hold substructs for all different items (WeaponInfo, ArmorInfo, ConsumableInfo)? This seems like it would be a waste of performance no?
How do I implement different items to work in the same inventory system?
Hey there, ideally WeaponInfo should derive from ItemInfo, that way you still have what’s in ItemInfo plus the weapon stuff. The problem is that in blueprints you can’t really do struct hierarchies.
Yeah I was thinking in BP that I might need to have ItemInfo consist of substructs. So for example, WeaponInfo would apply for WeaponBase, but not ArmorBase. What worries me about this method is I think it would mean all items would contain useless data. So Armor would have default values for WeaponInfo (despite not ever applying it). Would this cause bloating once compounded?
The problem is that in Blueprints aparently you can’t create sub-structs, so you can either create them in c++ or you need another approach. In my case every item i pick is the one i use, so i have the item as an actor and that has the information, then i have WeaponItem, ArmorItem, etc.
Wouldn’t that mean you would need to have identical functions for all items then? for example, if all items can be picked up, it would be redundant to make a pickup function for all possible clases rather than a singular parent class that could then allow children to share the functionality, no?
When i picked up items of the same type, and they could be stacked, i would merge them into the existing one (by addind the quantity) in the inventory and deleted the picked one.
Hi ! If you want to access a function / variable of a child from its parent, you need to use casting.
You can add for example an Enum in your ItemBase which will determine which kind of item it is ( weapon, consummable, etc … ), so you can easily cast to the right class in your logic.
Edit : You could even hold the subclass reference itself in your ItemBP info structure.