I try to set an inventory like in the game noita, my problem that i have difficulties to have a clean code
My player has 4 inventories each can stores a specific type of item :
1 array for weapons, player can hold 4 weapons
1 array for spells, player can hold 16 spells
1 array for cristals, player can hold 8 cristals
1 array for others, player can hold 8 others (potions, keys, food…)
My inventory is displayed on the screen i have 36 slots and i can interact with slots with the mouse.
Each slot returns :
His type (type weapon, type spell…)
His relative number(this number correspond to the array index)
For exemple the slot weapon 1 returns type–> weapon // relative number -->1
As you can see i need an huge amount of lines essentially because i have 4 differents arrays, i need to check in which type slot the mouse is in order to find the correct array to use (for update sprite, remove an item, swap an item…)
As above, most inventory systems have all items generally express a common base class, with different types of items handled in manners such as inheritance.
There’s of course different ways, such as composition or component based items, but inheritance is generally the easiest to start with.
Fair point… but I think I would just have a giant directory of ScriptableObjects for your skills, or inventories, or whatever.
Then I would just do a Resources.LoadAll<ScriptableObject>() and iterate them all.
“Are you a spell?” (ISpellCastable)
“Are you an item?” (IPersonalItem)
etc.
I think I’m just so data-lazy that way: “Hey, that directory over there, bathe those files and bring them to me now, I don’t really wanna hear much about the details. GO…”
I like databases as much as the next person, but often you need to serialise direct references in your design. Loot tables, crafting recipes, enemy loadouts, etc. To do so flexibly in Unity you need some way to reference these, usually with a common concrete type.
Inheritance being the principle mechanic of OOP, it really should be a starting point for learning. Anything else, components, interfaces, etc, are just built on top of that core principle. As they say in drawing, “You need to know anatomy before you can break it.”
Of course I don’t use straight inheritance in my own projects. One project uses component based items (using SerializeReference), which words fantastically.
I also recently put together a prototype-pattern object system as a bit of an experiment: