Redirect to the Docs main page.Docs

Holders NEW

Equipping Items in Player Inventory

Learn how to equip items in player holders dynamically using code.

This sample demonstrates how to equip items into player holders either by targeting any available holder or specifying a specific one. It's a helpful reference for integrating runtime equipment logic into your gameplay systems.

Overview

In this example, we show two main ways to equip an item:

  • Equip in any empty player holder (automatically finds the first available holder)
  • Equip in a specific player holder using its unique ID

To follow along, attach the sample script to a GameObject in your scene and assign an ItemDataSo in the Inspector.

Sample Code

using Inventory.Scripts.Core.Controllers;
using Inventory.Scripts.Core.Items;
using Inventory.Scripts.Core.ScriptableObjects.Items;
using UnityEngine;
 
namespace Inventory.Samples
{
    public class EquipInPlayerHolders : MonoBehaviour
    {
        [SerializeField] private ItemDataSo itemDataSo;
 
        private void Update()
        {
            // This creates the item instance. You don't need to do this in the flow if you're already 
            // working with an ItemTable instead of the ItemDataSo ScriptableObject.
            var itemInstance = StaticInventoryContext.CreateItemTable(itemDataSo);
 
            if (Input.GetKeyDown(KeyCode.J))
            {
                EquipInAnyEmptyPlayerHolder(itemInstance);
            }
 
            if (Input.GetKeyDown(KeyCode.L))
            {
                EquipInSpecificPlayerHolder(itemInstance, "62c55f0f-f2d1-4f46-83bd-58675d1a9652");
            }
        }
 
        public void EquipInAnyEmptyPlayerHolder(ItemTable itemToEquip)
        {
            var holderToEquip = StaticInventoryContext.InventorySupplierSo.FindPlayerHolderToEquipItem(itemToEquip);
 
            if (holderToEquip == null)
            {
                // Could not find a holder to equip the item.
                // Handle the scenario here.
                return;
            }
 
            holderToEquip.Equip(itemToEquip);
        }
 
        /// <summary>
        /// Specifies how you can equip an item directly into a specific holder.
        /// </summary>
        /// <param name="itemToEquip">The item to be equipped in the holder.</param>
        /// <param name="holderUid">The holder UID, which can be obtained from uiHolder.uniqueUid.Uid or holderData.id (they should match).</param>
        public void EquipInSpecificPlayerHolder(ItemTable itemToEquip, string holderUid)
        {
            var playerInventory = StaticInventoryContext.GetPlayerInventory();
 
            var holderToEquip = playerInventory.Holders.Find(holder => holder.id == holderUid);
 
            if (holderToEquip == null || holderToEquip.isEquipped)
            {
                // Could not find the holder to equip, or the item is already equipped.
                // Handle the scenario here.
                return;
            }
 
            holderToEquip.Equip(itemToEquip);
        }
    }
}

🔧 Adding Missing Support for FindPlayerHolderToEquipItem (Pre-1.5.0)

If you're using a version that doesn't include the FindPlayerHolderToEquipItem method, follow these steps to add the necessary functionality manually.

1. Extend StaticInventoryContext

Add the following method to your StaticInventoryContext script:

public static List<ItemHolder> GetPlayerInventoryItemHolders()
{
    var playerInventory = GetPlayerInventory();
 
    return Resources.FindObjectsOfTypeAll<ItemHolder>()
        .Where(holder =>
            holder != null &&
            playerInventory.Holders.Exists(data => data.id == holder.uniqueUid.Uid)
        )
        .ToList();
}

2. Add FindPlayerHolderToEquipItem to InventorySupplierSO

Next, in your InventorySupplierSO script, add the following method:

public ItemHolder FindPlayerHolderToEquipItem(ItemTable itemWillBeEquippedOrStored)
{
    return StaticInventoryContext.GetPlayerInventoryItemHolders()
        .Find(holder =>
            !holder.IsEquipped() &&
            holder.ItemDataTypeSo.Equals(itemWillBeEquippedOrStored.ItemDataSo.ItemDataTypeSo)
        );
}

[Optional] On pickup equip into a Holder if not equipped yet

If you'd like to automatically equip the item into a compatible holder (when not equipped already), replace your existing PickEnvironmentItem method with the version below:

public ItemTable PickEnvironmentItem(Holder itemHolder)
{
    var itemWillBeEquippedOrStored = itemHolder.GetItemEquipped();
 
    var holderToEquipUid = FindPlayerHolderToEquipItem(itemWillBeEquippedOrStored);
    var holderData = holderToEquipUid?.GetHolderData();
 
    // Equip item if a compatible holder was found
    if (holderToEquipUid != null && holderData != null)
    {
        holderData.Equip(itemWillBeEquippedOrStored);
        Destroy(itemHolder.gameObject);
        return itemWillBeEquippedOrStored;
    }
 
    // Fallback: Place item into the inventory grid
    var gridResponse = PlaceItemInPlayerInventory(itemWillBeEquippedOrStored);
    if (gridResponse != GridResponse.Inserted)
        return null;
 
    Destroy(itemHolder.gameObject);
    return itemWillBeEquippedOrStored;
}

Usage

  • Press J to equip the item in any available holder.
  • Press L to equip the item in a holder with a specific UID.

Make sure the UID you're using in EquipInSpecificPlayerHolder matches the ID of an existing holder in the player's inventory.

Notes

  • ItemTable represents an item instance created at runtime.
  • ItemDataSo is a ScriptableObject containing the static data for the item.
  • The system will not equip items into holders that are already occupied (isEquipped must be false).
  • FindPlayerHolderToEquipItem abstracts the logic of finding a suitable holder for a given item.

Need to reference how holders are configured or how ItemTable works? Check the Holders and Items documentation pages.

On this page