More detail on the solution I alluded to in my last post:
The Inventory
At it’s core, is just an array of structs that contains the data about the MyCollectableItem it represents. It should have all of the data about the actor in order to recreate it.
MyCollectableItem
A base class for all items that can enter the inventory. It can have functions like “OnInventorySelected” or “OnInventoryUse” or “OnCollected” that do custom behaviour, and can be called by the inventory manager / widget.
Inventory Interaction
Such as in your example, you are “Using” an inventory item. What this item does should be implemented in the inheriting MyCollectableItem blueprint/class (e.g. BP_HealthPotion). How can you get access to that information? You spawn the selected item from the class data saved in your struct (e.g. BP_HealthPotion) and call the “OnInventoryUse” function. The actor will perform its unique task (such as increase HP by 100).
Potential Flow:
- UI Selection of Inventory Item
- Spawn the relevant actor, give it the struct data if necessary. Save this actor reference.
- Tell the actor that it has been selected “OnInventorySelected” if necessary, (for example, equip the weapon)
- Player presses to “Use” the item. On the spawned actor, call “OnInventoryUse”, (for example, drink the health potion) remove this item from the inventory, or use 1 quantity if you have stacking items.
- Player selects a different Inventory Item - Destroy the saved actor reference, if valid, and repeat at step 1.