NOTE: Some users have found the the Group Item Restriction Object caused some issues due to overflow actions being triggered when items are rejected within a CanAdd function. This might have been a flawed design, it is recommended to avoid using overflow actions, or even better use custom Item Restrictions.

There are times where you want to apply a restriction to a subset of the Item Collections within an inventory. For example you may have an inventory with 3 Item Collections: Main, Equipped and Loadout. You could add a restriction to the Main and Equipped Item Collection which says that the inventory may only contain a single Sword item between the two Item Collections.

To achieve this you would use an “IItemRestriction”, the simplest one to use is GroupItemRestrictionObject. This is a Scriptable Object that can be created by right-clicking in the project view and select Create -> Ultimate Inventory System -> Inventory -> Group Item Restriction Object. Once created it may be assigned to an Inventory in the inspector.

There are a few settings which can be configured:

  • Item Collection IDs: The subset of Item Collections that will be affected by the restriction.
  • Item Collection Overflow: Specifies what happens if an item overflows the restriction. For example you may choose to reject the item or replace an old one instead. You can even choose to move items that where forcibly removed to another Item Collection or even apply item actions to them. A common use case for this is to add a drop action when an item is forcibly removed.
  • Item Collection Restriction: A mask of restriction types. Depending on the options selected some additional settings will appear. You can choose to restrict the amount of item which are part of a particular Item Definition or Item Category by setting a default stack size. For more control you can set the stack size on the Item Categories, Item Definitions or Items directly using the attribute system. If the attributes name match the one specified it will overwrite the default stack size for that restriction.

Custom Item Restrictions

To create your own restrictions is very simple, simply create a component which inherits the interface “IItemRestriction”. And set it next to the Inventory component.
During the Initialization phase the Inventory will find all the restriction components on its gameobject.

When the Inventory “Add Item Condition” function is called it will check all restrictions to see if it passes all the conditions before the item can be added or removed.

/// <summary>
/// Interface for item collection restrictions.
/// </summary>
public interface IItemRestriction
{
    /// <summary>
    /// Initialize the item collection.
    /// </summary>
    /// <param name="inventory">The inventory.</param>
    /// <param name="force">Force to initialize.</param>
    void Initialize(IInventory inventory, bool force);
    /// <summary>
    /// Can the item be added to the item
    /// </summary>
    /// <param name="itemInfo">The item info to add.</param>
    /// <param name="receivingCollection">The receiving item Collection.</param>
    /// <returns>The itemInfo that can be added (can be null).</returns>
    ItemInfo? AddCondition(ItemInfo itemInfo, ItemCollection receivingCollection);
    /// <summary>
    /// Can the item be removed from the itemCollection.
    /// </summary>
    /// <param name="itemInfo">The item info to remove (contains the itemCollection).</param>
    /// <returns>The item info that can be removed (can be null).</returns>
    ItemInfo? RemoveCondition(ItemInfo itemInfo);
}