Item
The Item class is the core of the inventory system. Items are simple objects (not a MonoBehavior or Unity Object) which can be unique or shared. The Terminology section explains the differences between the different item objects.
Items are created at runtime and contain attribute values that can be unique to that Item. Even though two Items have the same mold (Item Definition) they can be set to be slightly different. If the Item is part of a mutable Item Category it is even possible to change the values of the Item attributes at runtime.
Items do not have any “item specific logic” attached to it. It is meant to be a simple set of data. Using that data, which consists of the Item Category, Item Definition and Item attributes, the system knows what kind of actions can be performed on them.
Items can be mutable or immutable. Mutable items can be changed at runtime, whereas immutable items cannot be.
There is also a difference between Unique and Common items. Common Items will stack whenever possible while Unique items will always have an amount of one when added to an Item Collection.
Item API
Create Item by Name
// Create an Item from the Inventory System Manager by name. var potion = InventorySystemManager.CreateItem("Potion");
Create a Unique Mutable Item with a predefined ID
// If for some reason the item must have a predefined ID set it when you create the item. var armor = InventorySystemManager.CreateItem("Armor", 777); // For items to have different IDs they must be either Unique, Mutable or both. This is defined on the Item Category. var armorIsUnique = armor.IsUnique; var armorIsMutable = armor.IsMutable;
Create Item from an Item Definition
// Create an Item from the Inventory System Manager by Item Definition. var swordDefinition = InventorySystemManager.GetItemDefinition("Sword"); var sword = InventorySystemManager.CreateItem(swordDefinition);
Get an Attribute on an Item to get or set its value
// Change an attribute value on the sword item var attackAttribute = sword.GetAttribute<Attribute<int>>("Attack"); // Get the attribute value var attack = attackAttribute.GetValue(); // Setting the override value at runtime is only allowed on Mutable Items. attackAttribute.SetOverrideValue(attack + 5);
Make an Item copy to keep the same runtime values
// Make a copy of an existing item, copies the attribute values. var swordCopy = InventorySystemManager.CreateItem(sword);
Check if an Item is part of an Item Category or Item Definition
var weapon = InventorySystemManager.GetItemCategory("Weapon"); // Check if the item is part of a category var swordIsWeapon = weapon.InherentlyContains(sword); // Check if the item is part of a definition var swordIsSwordDefinition = swordDefinition.InherentlyContains(sword); // Similar function are available for checking if a definition is part of an ItemCategory. var swordDefinitionIsWeapon = weapon.InherentlyContains(swordDefinition); //It also works for checking children categories and definitions. var weaponChildIsWeapon = weapon.InherentlyContains(weaponChild); var swordDefinitionChildIsSwordDefinition = swordDefinition.InherentlyContains(swordDefinitionChild);
Add an Item to an Inventory
// Add an item to an inventory by name. m_Inventory.AddItem("potion", 5); // Add an item to an inventory by item definition. m_Inventory.AddItem(swordDefinition, 5); // Add an item to an inventory or for more control by item Info and item stack. var itemInfoToAdd = new ItemInfo(sword, 1); ItemStack itemStackDestination = null; m_Inventory.AddItem(itemInfoToAdd, itemStackDestination);
Add an Item to an Item Collection
// Get an item collection by name. var itemCollection = m_Inventory.GetItemCollection("Item Collection Name"); // Add an item to the item collection itemCollection.AddItem("potion", 3); // Add an item to an item collection by item definition. itemCollection.AddItem(swordDefinition, 5); // Add an item to an item collection or for more control by item Info and item stack. itemCollection.AddItem(itemInfoToAdd, itemStackDestination);
Get an Item from an Item Collection
// Get an item from the item collection, it returns a nullable Item Info. var itemInfoResult = itemCollection.GetItemInfo(sword); // If the item does not exist in the item collection the result will be null. if (itemInfoResult.HasValue == false) { // The item does not exist. } else { // The item was found in the Item Collection. var swordItemInfo = itemInfoResult.Value; // The Item Info has the amount and reference to the item stack. }
Check if an Item can be added to an Item Collection
// Check if the item can be added, the return type is a ItemInfo? (The ? means it is NULLABLE). var canAddItemResult = itemCollection.CanAddItem(itemInfoToAdd); if(canAddItemResult.HasValue){ if(canAddItemResult.Value.Amount == itemInfoToAdd.Amount ){ // The Item Amount can be fully added. }else{ // The Item can be added but only partially. } }else{ // The item cannot be added. }
Remove an Item from an Item Collection
// A simple remove. itemCollection.RemoveItem("potion", 3); // But most time you'll want more control. // Get the Item Info to remove var retrievedItemInfoResult = itemCollection.GetItemInfo(sword); if (retrievedItemInfoResult.HasValue == false) { // The item does not exist in the inventory. return; } // Use the retrieved item info to make a new item info with the amount of item you wish to remove. var itemInfoToRemove = (ItemInfo)(1, retrievedItemInfoResult.Value); // We can remove items from an inventory and it returns the item info that was actually removed. var itemInfoRemoved = itemCollection.RemoveItem(itemInfoToRemove); // Check the amount of the item Info removed to make sure the item was removed. if (itemInfoRemoved.Amount == itemInfoToRemove.Amount) { // The Item was removed successfully. }
Get all the Item, Item Definition and Item Categories in the system
// Get all ItemCategories, Item Definitions and Items in the game. var allItemCategories = InventorySystemManager.ItemCategoryRegister.GetAll(); var allItemDefinitions = InventorySystemManager.ItemDefinitionRegister.GetAll(); var allItems = InventorySystemManager.ItemRegister.GetAll();
Get all the Item Definition that are part of Item Category
//Get all ItemDefinitions of an Item Category var allMyCategoryItemDefinitions = new ItemDefinition[0]; myCategory.GetAllChildrenElements(ref allMyCategoryItemDefinitions, true); //Get all items loaded in the system that are part of the ItemCategory (this includes every single instances of items) foreach (var item in allItems ) { if (myCategory.InherentlyContains(item)) { //item is part of category } }