#InventorySystem ItemButton Display - Events or Direct Reference

1 messages · Page 1 of 1 (latest)

pearl panther
#

Recently I have started implementing an Item System into my game. The Items are stored into a ScriptableObject (InventoryItemDB) which can then be used to access the stored Items.

The current goal is to display the Inventory correctly, but I have several options on how I could do that:

  1. Let the ItemButton class have a function that fetches the Data from the InventoryItemDB based on an index variable that is assigned on start
    (Button1:index=0, Button2:index=1, etc.)
  2. Have an Event that is called whenever the UI should update. The event has the inventory as an argument and each itemButton,
    based on an index variable (see option 1), picks the correct item out of the inventory (using the index).
  3. Have a separate InventoryEventHandler that, when called, assigns each itemButton the correct itemData and then calls an itemButtonDisplayUpdateEvent (if required)

My goal here is to decouple the code as good as I can but also to stay on the performant side of things.
What would you recommend? Is there different approach to this as well? - Thanks in advance!

slow canopy
#

Ideally, your inventory UI would not directly interact with the Items, but rather with a sort of Container/Inventory component.

The most decoupled way I personally have implemented this, was by having a structure as follows:
Slot: Component responsible for displaying a single Item and passing user input to its associated Panel
Panel: Component for managing multiple Slots. Would have a reference to the Inventory
Inventory: Component that contains a list of Items (or more specifically, slot data, so information about Item, amount, durability, ... per inventory slot).
In my implementation, the Inventory had no knowledge of the Panel(s), but instead just fired events when the items inside changed, and the Panel(s) displaying that specific `Inventory's contents would react to those.

pearl panther
# slow canopy Ideally, your inventory UI would not directly interact with the Items, but rathe...

Ah okay so you add another layer in between! Meaning that whenever I want to communicate with the Item-Database I add a script handling the getting and assigning of the inventory Data (assigning to the ItemDisplayLogic) for example? I wish I could have come up with this myself... But thank you very much for that info! It might sound like I am overreacting but something like this is exactly what I have been looking for! I always thought that one Layer was enough for something like this but now that I know that and was wondering if I was doing things the right way. I cannot wait to use that kind of logic in my project!

Again thank you very much - I have learned a lot just from your message!
Have a nice rest of your day!

slow canopy
#

Meaning that whenever I want to communicate with the Item-Database I add a script handling the getting and assigning of the inventory Data (assigning to the ItemDisplayLogic) for example?
If I understand you correctly, that's what the Inventory would do. For example, if you were to add an Item to an Inventory, it would work somewhat like this: AddItem(item, amount): if contains item if any slot contains incomplete stack fill stack up by <amount> if remaining <amount> is greater than 0 add new slot content with remaining amountIn this case, item would be an object pulled from your InventoryItemDB and amount would be an int.

I have learned a lot just from your message!
Glad to hear that. 😄

#

Also let me add real quick, that there technically is yet another layer to my system (which wasn't relevant to your initial question though):
The InventoryManager component acts as a singleton and keeps track of the active ItemDatabase instance (the latter of which is a scriptable object), and allows other components (like the Inventory) easy access to item data.