Item Actions
Items can be stored but there are also actions you can perform on them, depending on your item’s settings. They’re all defined in the InventoryItem class, from which all scriptable object items inherit. By default, all these actions are blank, and it’s up to you to override them in your item classes.
Here are the possible actions that can be overridden to trigger stuff when they happen :
- Pick
- Use
- Equip
- UnEquip
- Swap
- Drop
For all these actions, you just need to implement what you want to happen in addition to the default action (adding the item to inventory on pick, equipping the item on equip, etc.).
Actions examples
The Inventory Engine comes with examples of how to override these methods. Here are a few of them.
Equip : When an armor gets picked in the PixelRogue demos, we call the Player’s SetArmor method, to reflect the equipped armor on its sprite.
public override bool Equip(string playerID)
{
base.Equip(playerID);
TargetInventory(playerID).TargetTransform.GetComponent<InventoryDemoCharacter>().SetArmor(ArmorIndex);
}
Use : This one comes from the Corgi Engine. When a Health item gets picked up, we check we have an owner (this is bound automatically by the CharacterInventory ability), we then get its Health component, and call the GetHealth method with the item’s HealthBonus value as a parameter, which increases its current health.
public override bool Use(string playerID)
{
base.Use(playerID);
if (TargetInventory(playerID).Owner == null)
{
return false;
}
Health characterHealth = TargetInventory(playerID).Owner.GetComponent<Health>();
if (characterHealth != null)
{
characterHealth.GetHealth(HealthBonus,TargetInventory(playerID).gameObject);
}
return true;
}
As you can see, implementing actions and effects for your items is very simple, and the possibilities are endless.
Accessing items
There are multiple ways to access items in an inventory. The UI layer of the engine provides direct hooks for that, and if you’d rather interact with your items via code, the Inventory class comes with all the methods you’ll need to find your items and target them. Depending on what you’re after, the methods you’ll use may differ, but usually you start by querying the inventory for the existence of your item :
List<int> myList = myInventory.InventoryContains(myItemID);
And then you can parse that list and do actions on the items (removing them, using them, etc), in this example we’re using that item for Player1:
for (int i=0; i<myList.Count; i++)
{
// myInventory.Content[myList[i]]; // that's an item
myInventory.Content[myList[i]].Use("Player1");
}
You can of course also use events to interact with your inventories. You’ll find events for every situation : Pick, Select, Click, Move, UseRequest, ItemUsed, EquipRequest, ItemEquipped, UnEquipRequest, ItemUnEquipped, Drop, Destroy, Error, Redraw, ContentChanged, InventoryOpens, InventoryCloseRequest, InventoryCloses, InventoryLoaded. Don’t hesitate to look at the API documentation, or the code and examples to learn more about how they work and how you can use them in your project.
Events are very easy to use, won’t require that you store a reference to your inventory, and can be called from other objects easily, they simply let the whole system (the part of it that is listening at least) that something happened. The following line, for example, triggers the pick of 8 units of the specified item :
MMInventoryEvent.Trigger(MMInventoryEventType.Pick, null, Item.TargetInventoryName, Item, 8, 0, "Player1");