Holders are a fundamental component in Ultimate Grid Inventory that manage the equipping, displaying, and interaction with items. They serve as the bridge between your game's items and their visual representation in the UI or game world.
All holder types in UGI inherit from the Holder base class, which provides the core functionality for managing equipped items. Understanding this base class is essential for working with any holder type.
Container Holders are special slots that can hold items like backpacks, chests, or pouches. When a container item is equipped in these holders, the container's contents become accessible.
The Container Holder's "Container Display Anchor SO" field
This connection ensures that when a container item (like a backpack) is equipped in the Container Holder, its contents will appear in the linked Container Display.
The ReplicatorItemHolder is a specialized UI holder that mirrors items from another holder. This is useful for displaying the same item in multiple UI locations, such as showing equipped weapons in both an equipment panel and a character preview.
Set the Unique Uid to a unique identifier for this holder
To set the item programmatically:
// Get a reference to the holdervar environmentItemHolder = GetComponent<EnvironmentItemHolder>();// Create an item table from an item data scriptable objectvar itemTable = StaticInventoryContext.CreateItemTable(itemDataSo);// Equip the item in the holderenvironmentItemHolder.Equip(itemTable);
The EnvironmentContainerHolder represents interactive containers in the game world (like chests, barrels, or lockers) that players can open to access their contents.
// Get a reference to the container holdervar containerHolder = GetComponent<EnvironmentContainerHolder>();// Open the container when the player interacts with itpublic void OnPlayerInteract(){ containerHolder.ToggleEnvironmentContainer();}
You can interact with holders programmatically using the InventorySupplierSo:
// Reference to the inventory supplier[SerializeField] private InventorySupplierSo inventorySupplierSo;// Reference to a holder[SerializeField] private Holder holder;// Equip an item in a holderpublic void EquipItemInHolder(ItemDataSo itemDataSo){ inventorySupplierSo.EquipItem(itemDataSo, holder);}// Unequip an item from a holderpublic void UnequipItemFromHolder(){ var itemTable = holder.UnEquip(); // Do something with the unequipped item if (itemTable != null) { Debug.Log($"Unequipped {itemTable.ItemDataSo.DisplayName}"); }}
To save and load the state of holders (what items are equipped), you can use the InventorySo class. For a comprehensive guide to the save system, see the Save System documentation:
// Reference to the inventory[SerializeField] private InventorySo inventorySo;// Save the inventory statepublic void SaveInventory(){ inventorySo.Save();}// Load the inventory statepublic void LoadInventory(){ inventorySo.Load();}
For environment containers, you can attach them to an InventorySo to automatically save and load their contents:
// Reference to the inventory[SerializeField] private InventorySo environmentContainersSo;// Reference to the environment container holder[SerializeField] private EnvironmentContainerHolder containerHolder;// Add the container to the inventory for saving/loadingprivate void Start(){ environmentContainersSo.AddHolder(containerHolder.GetHolderData());}// Remove the container when destroyedprivate void OnDestroy(){ environmentContainersSo.RemoveHolder(containerHolder.GetHolderData());}
Set up a save system to persist inventory state between game sessions
With properly configured holders, your inventory system will provide players with an intuitive way to manage their equipment and access container items, both in the UI and in the game world.