An Item Collection is a grouping of items. Items are stored in a list of Item Stacks where an Item Stack is a class with an item and an amount (not to be confused with an Item Amount which is a struct). Mutable items will always have an amount of 1 by default because they are unique. Immutable items can have any positive amount.

The purpose of the Item Collection can be specified within the inspector. This makes it easy to filter for a particular Item Collection based on the purpose of the Item Collection.

  • None: No specified purpose.
  • Main: Default collection in an inventory.
  • Secondary: Allows for custom use.
  • Equipped: The included items have been equipped.
  • Loadout: The collection is used for a loadout. The items won’t show in the UI.
  • Hide: Hides collections won’t show in the UI.
  • Drop: Used to drop items.

When you add and remove items it is recommended that you use the Item Info class. An Item Info does not only have an Item Amount but it also has a reference to an Item Collection and/or an Item Stack. This allows you to specify where an item came from before you add it. This is particularly useful combined with restrictions as it allows you to reject an item and give it back where it came from. This can also be used by the Inventory Monitor to ignore items that come from a loadout or from Item Collections contained within the same inventory.

You can also get all the items which are of a certain category or definition. For the most flexibility you can get exactly the Item Info that you want by defining your own filter and comparer when calling the GetItemInfos(…) function.

Example: Returning all of the ItemInfos that have mutable items and ordered by Item Definition name:

var filteredAndSortedItemInfos = itemCollection.GetItemInfos(ref itemInfos, 
x => x.Item.IsMutable, 
Comparer<ItemStack>.Create((i1, i2) => i1.Item.ItemDefinition.Name.CompareTo(i2.Item.ItemDefinition.Name)) );

The base Item Collection class comes with no special restrictions for its content apart that you cannot have non-positive amounts of an item, and you may only have a single amount of an item that is mutable.

You can create your own Item Collections and add restriction such as only X items can be stored or only items of a certain category, etc. Out of the box we offer a few generic Item Collections which can be setup for your own needs.

The Inventory component allows you to use Item Restrictions which apply restrictions on multiple Item Collections at the same time.

Item Collection API

//Find your item definition.
var myItemDefinition = InventorySystemManager.GetItemDefinition("MyItemDefinition");
//Create an item from that definition.
var myItem = InventorySystemManager.CreateItem(myItemDefinition);

//Or you can directly create an item from the item definition name.
var myOtherItem = InventorySystemManager.CreateItem("MyOtherItemDefinition");

//Make your item Info, Note double (( )) the (1,myItem) is an itemAmount.
//Note: ItemInfos can have a reference to the item stack and the itemcollection/inventory where the item comes from.
var myItemInfo = new ItemInfo( (1,myItem) );
itemCollection.AddItem(myItemInfo);

//Alternatively you can directly cast the item amount to an item info both are structs not classes.
//Note: It returns the item info added, this allows you to know the amount of item added and in which stack it was added to.
var itemInfoActuallyAdded = itemCollection.AddItem((ItemInfo) (1, myItem));

//You can get the first matching item stack to an item.var retrievedItemInfo = itemCollection.GetItemInfo(myItem);
if (retrievedItemInfo.HasValue == false) { //No item stack with that description was found in the inventory. }

//If you are interested in counting the amount of multiple stacks for matching description use GetItemAmount.
var amount = itemCollection.GetItemAmount(myItem);

//The boolean parameter lets us choose whether to check inherently or not.
var amountDefinition = itemCollection.GetItemAmount(myItemDefinition,true);

//We can remove items from an inventory and it returns the item info that was actually removed.
var itemInfoRemoved = itemCollection.RemoveItem((ItemInfo)(1,myItem));

//Specifying an item info that has a reference to an item stack will remove from that stack first, if possible.itemCollection.RemoveItem(retrievedItemInfo.Value);
//We could have chosen to remove a different amount, than the amount.
retrieved.itemCollection.RemoveItem( (1,retrievedItemInfo.Value) );

//You can get multiple items from your item collection.
//You'll need an array to store your item infos.
var array = new ItemInfo[0];
//You can use a pooled array instead of creating a new array each time.
IMPORTANT: make sure to return the pooled array once you finished using it.
var pooledArray = GenericObjectPool.Get<ItemInfo[]>();

//Set your filter and sort parameters, for this example we'll filter by category.
var filterParameter = InventorySystemManager.GetItemCategory("MyCategory");

//Item info list slice will have all the itemInfos in the inventory that contain inherits the category.
//You can set whatever filter parameter you want.
var itemInfoListSlice = itemCollection.GetItemInfos(ref pooledArray, filterParameter,
    //Specify your filter.
    (candidateItemInfo, category) => { return category.InherentlyContains(candidateItemInfo.Item); });

//IMPORTANT: Don't forget to return the pooled array once you finished using the array.
GenericObjectPool.Return(pooledArray);