Best approach in making Inventory UseItem functionality.

Hello everyone,

I’ve been trying things out lately and ran into one ‘issue’. I made an inventory system. Everytime you right click with mouse on the inventory slot that contains an item you get a menu with two options (Use, Drop).

Drop method is simple, but I’m not sure with Use, because I might be selecting any type of item (e.g. Weapon, Health kit, keycard) and these all items have different use methods (e.g. for Weapon I would like to equip the selected weapon, for health item I would like to heal, for keycard use it on a door if able).

Whenever I select the item and display the menu I send the Item scriptable object with all the item data. So it’s no telling what item it is. Well I could create an enum and specify the type and then do a switch and according to the type of item do something. But that could become really messy real quick.

What would be the decent / better approach in making this (Use functionality)?

Thank you!

Hi, the usual approach to solve this problem it’s inheritance. You derive your base Item class which have a virtual method (i.e. void Use()) and override It to achieve the desired effect. Then when in your UI Use is requested you call Use() and the correct method it’s executed.

What I’ve been doing recently it’s making and abstract ScriptableObject called Interaction with an abstract method called Interact. Then I make all my Interactions as children (Start Dialogue, Add item to inventory…) and set it all up from within Scriptable Object turned into assets. Then my Items have an Interaction field called OnUse and I attach the desired Interaction to take place.

Hope that helps!