Redirect to the Docs main page.Docs

Scripting

Merchant System

Learn how to implement a merchant system using ItemGrid2D and inventory management.

Merchant System

The Merchant script provides functionality for creating a merchant system where players can buy and sell items using grids. This system includes a merchant grid for items available for purchase and a sell grid where players can place items they wish to sell.

Overview

The script interacts with ItemGrid2D, InventorySo, and InventorySupplierSo to manage item placement and transactions. Below are the key components:

using Inventory.Scripts.Core.Grids;
using Inventory.Scripts.Core.ScriptableObjects;
using Inventory.Scripts.Core.ScriptableObjects.Items;
using UnityEngine;
 
namespace Inventory.Samples
{
    public class Merchant : MonoBehaviour
    {
        [SerializeField] private ItemGrid2D merchantGrid; // Where will contain the items that the merchant is selling.
        [SerializeField] private ItemGrid2D sellGrid; // Grid which player will put items to sell to the merchant
        
        // Merchant Items
        [SerializeField] private ItemDataSo[] merchantItems;
        
        // Handlers
        [SerializeField] private InventorySo playerInventory;
        [SerializeField] private InventorySupplierSo inventorySupplierSo;
 
        private void OnEnable()
        {
            // Here you can insert the items on the merchantGrid, will be the merchant items.
            var merchantGridItems = merchantGrid.Grid;
 
            foreach (var itemDataSo in merchantItems)
            {
                // Maybe you want to get the response in order to do something with it... Up to you
                inventorySupplierSo.PlaceItem(
                    itemDataSo,
                    merchantGridItems
                );   
            }
        }
 
        // Trigger this once player clicks in a "Sell Button"
        public void SellItems()
        {
            var sellerGrid = sellGrid.Grid;
 
            var allItemsFromGrid = sellerGrid.GetAllItemsFromGrid();
            
            foreach (var itemSoldByPlayer in allItemsFromGrid)
            {
                itemSoldByPlayer.RemoveItselfFromLocation();
                
                // Here you can give player money or something...
                // If money is an item you can do something like this:
                // the 'itemDataSoFromMoney' can be a serializable field from type ItemDataSo...
                
                // inventorySupplierSo.FindPlaceForItemInGrids(
                //     itemDataSoFromMoney,
                //     playerInventory.GetGrids()
                // );
            }
        }
 
        // This an example on how to rollback the items to the player inventory.
        private void OnDisable()
        {
            RollbackSellingItemsToPlayerInventory();
        }
 
        // Method to return items to player inventory in case, player closes the UI or for some other reason...
        private void RollbackSellingItemsToPlayerInventory()
        {
            var sellerGrid = sellGrid.Grid;
 
            var allItemsFromGrid = sellerGrid.GetAllItemsFromGrid();
            
            foreach (var itemTable in allItemsFromGrid)
            {
                inventorySupplierSo.PlaceItemInPlayerInventory(itemTable);
            }
        }
    }
}

Usage

  • Attach the script to a GameObject in your scene.
  • Assign the required grids (merchantGrid, sellGrid).
  • Populate the merchant's inventory by assigning merchantItems.
  • Ensure the inventory supplier and player inventory are correctly linked.
  • Call SellItems() when the player confirms a sale.

Notes

The script does not handle currency directly, but you can modify the SellItems method to implement rewards.

If the merchant UI is closed before a transaction is completed, items are safely returned to the player's inventory.

This system can be extended to include buying items from the merchant by implementing a similar logic in reverse.

Conclusion

The Merchant System provides a structured way to implement item transactions in an inventory grid. It ensures seamless item management while allowing for easy modifications to support different game mechanics.

On this page