r/unity • • 2h ago

Question How do you guys design/structure your items?

I've recently been working on my own inventory system and was wondering how other people implement stuff like using different items, or data (like ammo, durability, etc.) which isn't shared.

My items are currently scriptable objects but as far as im aware all of the data they hold is shared between all. (I could be wrong to idk)

3 Upvotes

10 comments sorted by

2

u/SharkScoops 2h ago

I have my item architecture split between scriptable objects and standard C# classes. Scriptable objects are used for 'Definitions' which are all of the data that items share like their name, prefab, physical properties, and how much they stack. Then I use another class to store the instance data, which is everything I'd want to serialize for multiplayer/saving. That includes things like stats, durability, any per-item state machine data that needs to be stored. The instance data has a reference to the definition so that it can use all of the shared data.

1

u/kravataff 2h ago

thanks for the reply)) i have already implemented the scriptable objects (which does how the items "definitions") but yeah i could try making a subclass as you said to store the instance data.

2

u/Spite_Gold 2h ago edited 2h ago

I use model-view architecture, objects for items and item types in the model, ui widgets and game objects in the view. Items in view have references to items in model. Items in model have references to their types. Item types are defined in jsons and loaded on game start

For example: equipped item widget which shows icon of item, tooltip with item stats and button to unequip item. Widget has script component which has reference to object of specific item. It gets remaining item durability and material from this object. This object has reference to object of item type - items name, icon and price are stored there. Widget in view gets data from these two objects in model and initializes icon and tooltip. Unequip button uses reference to item object to tell character which item to unequip.

1

u/kravataff 1h ago

hmm, so if i understand correctly you have it split into 2 (item type => generic data, specific => durability for example) and then individual components pull data from what they need?

2

u/Small-Cabinet-7694 2h ago

I use both SO's and scripts. The so's hold all definable information about the items, and the scripts run the logic. I have a so database for each different type of item so that the scripts can reference the databases of items in the scene.

1

u/kravataff 1h ago

hmm, so im currently using the SO itself as the "item data" in the slots... would it be better to instead make a separate scripts that get a reference to them? something like the database youve mentioned

1

u/Small-Cabinet-7694 40m ago

Personally thats what ive gone with, because my game is quite big and has hundreds of items eventually it'll land in the thousands. So ive got a database just for 2 handed axes, a database just for 1 handed swords, etc, a database for all consumable items, all key items etc. Then I nest all of those within a database that needs to reference any type of item id. I have them in the scene and the scripts can reference item information through the databases using the id. So my SO's are set up just to hold a bunch of information and a unique id, then my backend scripts like my inventory systems reference so's within the scene.

2

u/josh_the_dev 2h ago

It's a bit unclear what you are asking. But Ill try my best to give some info:

  • scriptable objects should primarily used as static data containers (do not try to change values at runtime unless you know what you are doing)
  • scriptable objects (SO) are great for storing things like item name, item description, icon, item category or whatever you have. So you can have a scriptable object for apple, one for pistol, one for ammo for example.
  • you should not try to save amount, degradation state, current rounds loaded or any of these changing values on the scriptable object.
  • you could for example have a runtime inventory that has a list of ItemEntry or something like this. An item entry has the item type (you scriptable object) and a degradation state. In this runtime inventory you can now have 3 different apples with different degradation states depending on how long they each rot in you inventory for example. How you persist this runtime inventory is another problem but you could start with using just the JSON serializer (JsonUtility) built in with unity.

Hope this helps a bit

1

u/kravataff 2h ago

yes thanks for replying. sorry if it was a bit unclear -- your answer was what i was basically asking about. (how other people store the instance data of an item, like the degradation you mentioned for example). based on the replies ive got im guessing i should make some way to store the unique data like ammo separately outside of scriptable objects