In many games Items can be equipped and/or used to affect the character stats and/or the weapon the character is carrying. Using the attribute system you may add any data on an Item, how this data is used depends completely on the type of game one is making.

Here are a few solutions/ideas to inspire you, they are not the only options available so do not feel constrained by them.

Character stats (Equipper)

The most obvious way to affect the character stats is when an item is equipped. The Equipper component is a good start for visually equipping items, it also has a very convenient event that is executed when an Item is equipped or unequipped. Listen to the “EventNames.c_Equipper_OnChange” event to know when the character stats must be updated. A very similar solution to the one below is actually used in the demo scene.

/// <summary>
/// Example of character stats.
/// </summary>
public class ExampleCharacterStats
{
    protected IEquipper m_Equipper;
    protected int m_BaseMaxHp;
    protected int m_BaseAttack;
    protected int m_BaseDefense;
    
    protected int m_MaxHp;
    protected int m_Attack;
    protected int m_Defense;
    public int BaseMaxHp => m_BaseMaxHp;
    public int BaseAttack => m_BaseAttack;
    public int BaseDefense => m_BaseDefense;
    public int MaxHp => m_MaxHp;
    public int Attack => m_Attack;
    public int Defense => m_Defense;
    /// <summary>
    /// Constructor.
    /// </summary>
    /// <param name="baseStats">The base stats.</param>
    /// <param name="equipper">The equipper.</param>
    public ExampleCharacterStats(int maxHp, int attack, int defense, IEquipper equipper)
    {
        m_BaseMaxHp = maxHp;
        m_BaseAttack = attack;
        m_BaseDefense = defense;
        m_Equipper = equipper;
        if (m_Equipper != null) {
            EventHandler.RegisterEvent(m_Equipper, EventNames.c_Equipper_OnChange, UpdateStats);
        }
        UpdateStats();
    }
    /// <summary>
    /// Update the character stats.
    /// </summary>
    public void UpdateStats()
    {
        m_MaxHp = BaseMaxHp + m_Equipper.GetEquipmentStat("MaxHp");
        m_Attack = BaseAttack + m_Equipper.GetEquipmentStat("Attack");
        m_Defense = BaseDefense + m_Equipper.GetEquipmentStat("Defense");
    }
}

The Equipper comes with a useful GetEquipmentStat(“AttributeName”) function that finds the attribute on all the equipped items and adds them together in one value.
It is important to recompute the stat each time as adding and removing float values over and over game may start deviating from the correct value due to floating precision errors. This is why it is advised to keep the base stats separate from the weapon stats.

Of course instead of using the GetEqupimentStat(“AttributeName”) function you may get the attributes manually to compute the stats however you wish, whether is a percentage, special effect (poison, fire, etc..), or anything really. Learn more about attributes here.

You may learn more about the Equipped component and how to equip weapons here.

Note that you are free to create your own Equipper script from scratch, you are not restricted to the solution we provide.

Weapon stats (Item Binding)

Some times instead of changing the character stats you may be interesting in changing the Weapon stats. Depending on your game the object dealing damage can either be the character or the weapon.

The easiest option to affect weapon stats is to use an Item Binding object. When spawning/equipping a weapon game object it is highly recommended that the weapon has an Item Object component which can be used to bind the game object to an Item. When the binding happens a “EventNames.c_ItemObject_OnItemChanged” is executed. You may listen to this event to deal with a new item being bound to the equipped item. You may use this event to change model, vfx, sound, ect… and of course it can also be used to change stats.

For convenience we added the Item Binding component which lets you bind attribute values from the bound item to any component on the Weapon. And this can be done entirely in the inspector without a single line of code! To learn more about the Item Binding object go here

Consumable stats (Item Action)

There are times where a non-equippable item can be used to affect stats. A good example is food which can be used to heal the character or give with bonuses for X seconds. For this it is recommend to use a combination of custom Item Actions and Monobehaviors. The Item Action can be used to call a function on the custom Monobehavior. For example an “Heal Item Action” which calls the function “Heal” on a custom “CharacterHealth” component.

To learn more about Item Actions go here and to learn more about how to use items as skills go here

Other/Custom

There are many other ways items can be used to affect the player character stats and/or the weapons the character equips. For example equipping a Bow and swapping the Arrow to give different effects (normal, fire, poison, etc..).

These more advanced use case require a bit more custom functionality but the Item and Attribute system makes it easier to scale than implementing such a system from scratch.