0
\$\begingroup\$

Currently, I have an Inventory that is a ScriptableObject, which means it's not tied to any specific game object or player character. I just drag it into a MonoBehaviour if I would like expose the data of the inventory.

I have created an InventoryUI, which is a MonoBehaviour attached to a GameObject in my scene, and I assign the Inventory SO to it.

The InventoryUI contains button GameObjects with an InventorySlot script, to which I assign the item each slot is holding.

Everything works fine. When I click on one of the InventorySlot buttons, I'm able to call item methods, such as UseItem.

My problem is if the the item is a potion, for example. How can i add health to the player? Of course I could add reference to the player to the slot. But that will reference player repeatedly over 25 slots.

\$\endgroup\$
  • \$\begingroup\$ What about using triggerEvents on your inventory slot sprites? hook them up to a method that want to execute on use. \$\endgroup\$ – formatc2013 Mar 24 at 17:13
  • \$\begingroup\$ Can you provide any example? \$\endgroup\$ – Printer Mar 24 at 18:10
1
\$\begingroup\$

Myself, I'd let the InventoryUI script own the interaction with the inventory.

When a player opens the InventoryUI, you call something like InventoryUI.ShowInventoryFor(PlayerCharacter character).

This procedure handles any initial setup of the menu (populating the slots, etc.) and caches the character it was given as its currentUser member variable.

Each of the UI buttons on the menu is wired up to call InventoryUI.UseItemInSlot(int slotID) when clicked. That centralizes all of the item use actions onto the one script that knows who's using the menu.

Now the InventoryUI can fetch the corresponding item in your inventory structure, and call inventoryItem.UseItemOn(currentUser), passing its knowledge of who is using the item through to the function that performs the item's action.

Finally, that item receives a reference to the character it should modify - by giving it more health, etc.

Doing it this way, you might not need your InventorySlot script at all. The buttons in the menu can be just plain buttons, with the knowledge of the meaning behind each button provided by the InventoryUI itself.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you sir, this made it more clear how i should set it up \$\endgroup\$ – Printer Mar 25 at 8:12

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.