How do I implement different items to work in the same inventory system?
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?
asked Jan 17 '18 at 04:55 AM in Blueprint Scripting
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.
answered Jan 17 '18 at 02:47 PM
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.
Follow this question
Once you sign in you will be able to subscribe for any updates here