Events

The inventory system uses two types of events. It uses the C# event system as well as the Opsive Event Handler which is used throughout the set of Opsive assets.

Important: All of the Event Handler events can be found within the EventNames.cs file. Event Handler event names are static constants which are used throughout the system. The names have a particular syntax such that can be easier to undertsand what the target and parameters are. If unsure how the event should be handled check the comments in the EventNames.cs file or by finding where the event is being executed:

public const string c_TargetObjectType_EventDescription_Parameter1Type_Parameter2Type_...

public const string c_ItemCollection_OnAdd_ItemAmount

// Global events do not have a target and have the syntax below.
public const string c_Global_EventDescription_Parameter1Type_Parameter2Type_...

 

The code below will register, execute, and unregister from a particular event. The parameter is a simple “int”. Both the event name and the parameter type must match for the event to be listened to properly.

 

//Generic Examples

// Includes the correct namespace.
using Opsive.Shared.Events;

// Registers for myEventName on myObject with a single int parameter. When the event is sent the MyFunction will be executed. 
EventHandler.RegisterEvent<int>(myObject, myEventName, MyFunction);

// Executes myEventName with an int parameter on myObject.
EventHandler.Execute<int>(myObject, myEventName, 7);

// Unregisters from the event.
EventHandler.UnregisterEvent<int>(myObject, myEventName, MyFunction);

 

It is important to understand that the event is executed with a list of parameters (can be none), the listeners (when using RegisterEvent) method handler needs to match the parameters type exactly. If not an error will occur.
Therefore the Event Handler Execute function defines the pattern that should be used for that event name. You should never execute the same event name with different parameter types.
You may create your own events by following the examples above, or you may simply listen to the events we execute throughout the system.

Examples Specific to the Inventory System

 

//Examples Specific to the Inventory System
The code below will register, execute, and unregister from a particular event.

/// <summary>
/// Register to events on awake or on start.
/// </summary>
private void Start(){

    // Listen to the inventory add item event.
    // The first parameter is the target, in this case the inventory.
    // The second parameter is the event name which can be found in the EventNames static class.
    // The third parameter is the function that will be called when the event is executed. 
    // Note the function must have the parameters ItemInfo and ItemStack as specified in the '<>' of the event execution. In this case it is executed in the ItemCollection class
    EventHandler.RegisterEvent<ItemInfo, ItemStack>(m_Inventory, EventNames.c_Inventory_OnAdd_ItemInfo_ItemStack, HandleOnAddItem);

    // Listen to the inventory remove item event.
    EventHandler.RegisterEvent<ItemInfo>(m_Inventory, EventNames.c_Inventory_OnRemove_ItemInfo, HandleOnRemoveItem);

}

/// <summary>
/// Handle the item being added to the inventory.
/// </summary>
/// <param name="orignItemInfo">The origin of the item that was added.</param>
/// <param name="addedItemStack">The item stack where the item was added.</param>
private void HandleOnAddItem(ItemInfo orignItemInfo, ItemStack addedItemStack){
	 if (addedItemStack== null) { return; }
	 //use the orignItemInfo and addedItemStack to know where the item came from, where it was added, how much was added, etc...
}

/// <summary>
/// Handle the item being removed.
/// </summary>
/// <param name="removedItemInfo">The removed Item info.</param>
private void HandleOnRemoveItem(ItemInfo removedItemInfo){
	 //The removedItemInfo contains the amount that was removed in Amount and the amount that is still left in ItemStack.Amount
}

/// <summary>
/// Make sure to unregister the listener on Destroy.
/// </summary>
private void OnDestroy()
{
     EventHandler.UnregisterEvent<ItemInfo, ItemStack>(m_Inventory, EventNames.c_Inventory_OnAdd_ItemInfo_ItemStack, OnAddedItemToInventory);
     EventHandler.UnregisterEvent<ItemInfo>(m_Inventory, EventNames.c_Inventory_OnRemove_ItemInfo, OnRemovedItemFromInventory);
}