Redirect to the Docs main page.Docs

Scripting

How to Integrate with Grids and Holders

Learn how to programmatically interact with Grids and Holders in Ultimate Grid Inventory using the InventorySupplierSo.

Integrating with Inventory Grids and Holders

The Ultimate Grid Inventory (UGI) system provides a powerful Scriptable Object called InventorySupplierSo that makes it easy to programmatically interact with inventory grids and holders. This guide explains how to use this tool to manage items in your game code.

What is InventorySupplierSo?

InventorySupplierSo is a utility Scriptable Object that:

  • Provides methods for finding available spaces in grids
  • Handles item insertion and removal from inventory grids
  • Manages item transfers between different inventory containers
  • Validates item placements based on grid rules and constraints

Adding Items to Inventory Grids

Here's a complete example of how to add an item to a player's inventory:

using Inventory.Scripts.Core.Enums;
using Inventory.Scripts.Core.ScriptableObjects;
using Inventory.Scripts.Core.ScriptableObjects.Items;
using UnityEngine;
 
public class AddItemToGrid : MonoBehaviour
{
    [SerializeField] private InventorySo playerInventory;
    [SerializeField] private InventorySupplierSo inventorySupplierSo;
    [SerializeField] private ItemDataSo itemWhichWillBeInserted;
 
    public void InsertItemOnPlayerInventory()
    {
        // Try to find a place for the item in any of the player's inventory grids
        var (item, gridResponse) = inventorySupplierSo.FindPlaceForItemInGrids(
            itemWhichWillBeInserted,  // The item we want to add
            playerInventory.GetGrids() // All available grids in the player's inventory
        );
 
        if (gridResponse == GridResponse.Inserted)
        {
            Debug.Log("Item was successfully added to inventory!");
            // You can trigger additional effects or UI updates here
        }
        else
        {
            Debug.Log("Could not add item - inventory might be full");
            // Handle the failed insertion (show message, drop item in world, etc.)
        }
    }
}

Key Methods in InventorySupplierSo

The InventorySupplierSo provides several useful methods:

  • FindPlaceForItemInGrids: Attempts to place an item in the first available spot across multiple grids
  • PlaceItem: Places an item in a grid and returns the result
  • RemoveItem: Removes an item from a grid or holder

Note: To check if an item can fit in a grid or to retrieve all items from a grid, you should use methods on the GridTable object:

  • GridTable.FindSpaceForObjectAnyDirection(): Checks if an item can fit in a grid and returns the position
  • GridTable.GetAllItemsFromGrid(): Retrieves all items currently in a grid

Next Steps

After implementing basic inventory interactions, you can:

  • Create custom item management systems
  • Build crafting mechanics that consume and produce items
  • Implement loot systems that add items to the player's inventory
  • Design shop interfaces that transfer items between inventories

For more advanced usage examples, check the Creating a Crafting System guide.

On this page