- Home /
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!
Answer by Pakillottk · Sep 30, 2018 at 12:24 AM
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!
Hey, thanks for the suggestion! Your suggestion seems interesting, tho what I thought about using is Events more specifically UnityEvents. So what I actually ended up making is adding UnityEvent to the Item class. Then inside a curtain class (e.g. WeaponSwitch) I created an array of Items, where I assigned all the Weapon Items and OnEnable added Equip method Listener to the Use UnityEvent. And OnDisable removed all listeners from the Use UnityEvent of course. When I open the menu I grab a reference of the selected item and whenever I press 'Use' button I just simply access item's Use UnityEvent and simple invoke it and that Invokes all added listeners. I don't know if this is the best way in making something like this, but it works quite well tbh!
That's an interesting solution. The only thing I'd look into it's switching from UnityEvent to native c# events, because these are more perfomant and given that you are doing everything by code I think that you can afford losing the convenient Inspector editor form UnityEvents.
Your answer
Follow this Question
Related Questions
Deleting item from inventory system... 1 Answer
Grid Inventory : Move Item & Slot Highlighting 2 Answers
Invoking Effects of (Random) Items (C#) 0 Answers
[C#]Inventory script help. 3 Answers