Hello fellow coders.
I have recently started working on an inventory system.
As the main feature, I am using an
enum that is meant to help me in dividing the objects of the inventory so that later I can do queries based on the
enum type/value and display those results.
The issue I am facing is that I cannot seem to find a way to pass the
enum when I am adding the item to the inventory.
Although I can add the item if I hard code it.
I am starting to think that this is not the best solution to use (since I am stuck);
I am also aware that I could use a string for the types of queries that I am to make, but when set, it is more prone to misspelling and gives me an uneasy feeling.
Why I would need it to be set dynamically you may ask?
Because I don’t want to write for every
ScriptableObject I will create the same lines of codes.
It feels redundant.
I am looking forward to your inspiration. Thanks in advance!
Instead of enums, you could use instances of an object of some type to identify ItemType. It gets better, since you can use this object to carry the shared info about the item, like its image, display name, max. stack size, cost etc. and since we are in Unity, you can create an ItemType class which is a ScriptableObject, then define your ItemTypes using the inspector, by assigning the image, text, etc.
You can even use inheritance to have more complex ItemTypes carry more specific information or implement some virtual methods / properties that define some interactions with the ItemType.
Don’t store the item image in your inventory together with its type and name, just store the reference to the ItemType ScriptableObject instance + count.
You can’t change the values of an enum at runtime, they’re hardcoded, instead you can use lists, which can store anything from gameobjects, to integers, to floats, basically anything, you can change their values dynamically at runtime.