Action Modules & Groups
Action Modules and Groups
ItemActions were built with modularity and flexibility in mind. Everything related to if, when and how an item is used can be defined as ActionModules.
The ItemActionModule class inherits the BoundStateObject class. The BoundStateObject class allows modules to be affected at runtime by both states and bindings.
Bindings are used to override values with variable values.
ActionModuleGroups are generic objects that only accepts a certain type of ActionModule. The common ActionModuleGroups for all UsableItemActions are the Trigger and Usable groups.
InvokeOnModulesWithType<IMyModuleInterface>(module => module.MyInterfacefunction());
There are overloads to pass in arguments without creating any allocations:
InvokeOnModulesWithType<IMyModuleInterface>((module, i1) => module.MyInterfacefunction(i1));
/// <summary> /// Updates the registered events when the item is equipped and the module is enabled. /// </summary> /// <param name="register">register or unregister?</param> protected virtual void UpdateRegisteredEventsInternal(bool register) { }
/// <summary> /// The item was picked up. /// </summary> public virtual void Pickup() { } /// <summary> /// The item will be equipped. /// </summary> public virtual void WillEquip() { }
/// <summary> /// The item was equipped. /// </summary> public virtual void Equip(){ } /// <summary> /// The item will start unequipping. /// </summary> public virtual void StartUnequip() { } /// <summary> /// The Item was unequipped. /// </summary> public virtual void Unequip() { } /// <summary> /// The item was removed from the character. /// </summary> public virtual void RemoveItem() { } /// <summary> /// Clean up the module when it is destroyed. /// </summary> public virtual void OnDestroy() { }
The ‘UpdateRegisteredEventsInternal’ function is particularly important as it allows the ItemAction to listen to certain events only while the Item is equipped and the module is enabled. It is a good place to listen to AnimationEventTrigger events or ability events.
Custom Item Action Modules
When making a custom ItemActionModule the first thing to do is identify in what group you would like your custom action to be part of.
/// <summary> /// A simple module which has a boolean to enable can start. /// </summary> [Serializable] public class DebugUse : UsableActionModule, IModuleUseItem { [Tooltip("The message to print on Use")] [SerializeField] private string m_Message = "Hello :)"; /// <summary> /// Use the item. /// </summary> public void UseItem() { Debug.Log(m_Message); } }