Item Object Behaviour Handler

Item Object Behaviours Handler is a component which sits next to an Item Object and is used by the Equipper to use equipped items. It allows you to easily trigger Item Object Behaviours through different inputs. A Item Object Behaviour Handler can have multiple Item Object Behaviours linked to it. For example if you have a “Gun” category you can specify an action to fire, reload, aim, etc.

Item Object Behaviours are components that you can add on your Item GameObject. You can add as many as you wish. In the demo scene the character attacks with the “Fire1” button (left button click by default) when a sword or a staff is equipped. Looking at the Usable Weapon prefabs you’ll see that the action does not have an input assigned to it.

The Item Object Behaviours can be triggered through code, this is usually done by the Equipper component which listens to the Inventory Input component. 

The Item Object Behaviour components are game specific so you will likely need to implement your own. The base class Item Object Behaviour has two methods: CanUse and Use. By default CanUse will be true if Time.time >= m_NextUseTime. This means you can setup a cooldown by setting m_NextUseTime = Time.time + coolDown when the action is used. If you wish to have more control on when an item can be used, you can override the CanUse method.

 

/// <summary>
/// An abstract class used to create actions for item objects.
/// </summary>
public abstract class ItemObjectBehaviour : MonoBehaviour
{
    protected float m_NextUseTime;
    /// <summary>
    /// Can the item object be used.
    /// </summary>
    public virtual bool CanUse => Time.time >= m_NextUseTime;
    /// <summary>
    /// Use the item object.
    /// </summary>
    /// <param name="itemObject">The item object.</param>
    /// <param name="itemUser">The item user.</param>
    public abstract void Use(ItemObject itemObject, ItemUser itemUser);
}

 

If you choose to override the Use method you will be given two parameters, the Item Object being used and the Item User (usually on the character). Using these you can get the item attributes (or the Item User’s stats) to customize what happens when the item is used.

In the demo we use the character stats to modify the damage when attacking. We also modify the number of projectiles coming out of the character’s staff and change the projectile prefab.

Tip: Item Object Behaviours go really well with Item Bindings, Item Bindings are used to bind item attributes to properties on any component.