1
\$\begingroup\$

I'm trying to create an inventory system. There's a few parts to it:

  • Item (scriptable Object just the data like name etc of the item)
  • ItemDatabase which takes array of Item (scriptable object) and then on awake it looks through and populates a dictionary for ID,data
  • ItemStack - Actual item holds an int for an ID so it can get data from the dictionary from it.

My inventory has a list of ItemStacks, but to be able to populate that array the script needs to be on a GameObject, so with how everything works, is it required for me to always have GameObjects in the game basically representing items? I'm new to inventories so is that how its suppose to be? Or am I missing something here? It seems kinda strange if I have 700 different item in my inventory that would require 700 GameObjects created to represent them.

Or maybe I'm overthinking it... but I feel like the inventory should only have "data" and not a GameObject, the GameObject would be for when you spawn an item in the world.

\$\endgroup\$
  • \$\begingroup\$ "but to be able to populate that array the script needs to be on a GameObject" in what sense is this required? What specifically prevents you from representing your items with data structures that are not attached to GameObjects? \$\endgroup\$ – DMGregory Mar 13 at 2:41
  • \$\begingroup\$ I won't lie I sent this and sat on it for like 20 mins and realized the reason I couldn't get scripts or "classes" to work without being on a gameobject was cause they were inheriting from monobehavior, I feel kinda dumb but at the same time I'm glad I've gotten this problem out of the way lol \$\endgroup\$ – Kimbur Mar 13 at 2:52
  • 1
    \$\begingroup\$ If you've solved your problem, please post your solution as an answer — it might be useful to other developers who run into similar troubles. :) \$\endgroup\$ – DMGregory Mar 13 at 2:56
3
\$\begingroup\$

I've figured out that the reason that I couldn't populate the inventory with only data and needed to be on Gameobject was cause the class was inheriting from Mono-behavior which REQUIRES it to be on a gameobject the solution was just to make the class not inherit from it.

\$\endgroup\$
0
\$\begingroup\$

My solution for this in my last game was to have three different classes:

  • ItemData which inherited from ScriptableObject and contained all the relevant data for a type of item, both mechanical and regarding its appearance, both in the game world and in the inventory.
  • FloorItem which inherited from MonoBehaviour and represented an item laying on the floor in the game world.
  • InventoryItem which inherited from MonoBehaviour and represented an item in my inventory system UI.

Both FloorItem and InventoryItem had an itemData property referencing the scriptable object determining its type. When that property changed, the behaviours also changed the sprite/mesh, description text etc. to match the new data. I made this an editor script so I was able to change the type of an item in the game world by simply dragging another ItemData asset into it.

The ItemData class could also serve as a factory. So when the player picks up an item, I could do floorItem.itemData.createInventoryItem() to create a new UI object to insert into my inventory UI. And vice versa, when the player dropped an item from the UI, I could do inventoryItem.itemData.createFloorItem() to instantiate a new GameObject in the world.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.