Item Stats
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.
For an in engine example, please refer to the feature scene “14 Get Set Attribute Stat (With Code)” which showcases how to get and set attribute values, and how you can use attributes to compute stats in your own code.
Here are a few solutions/ideas to inspire you, they are not the only options available so do not feel constrained by them.
Item Collection or Item list Float and Int Sum
The ItemCollection comes built-in with a function for getting a sum of all Int and Float attributes with the same name of all items in the collection. This is a very easy way of getting the sum of the equipment stat.
var attributeName = "MyStat"; // Get the inventory reference. var inventory = InventorySystemManager.GetInventoryIdentifier(1).Inventory; var itemCollection = inventory.GetItemCollection("MyItemCollection"); var statSum = itemCollection.GetFloatSum(attributeName);
In some cases you might have a list of Item, ItemInfo, ItemStack or ItemDefinition. In that case you can use the AttributeUtility static class to get a sum of the attributes.
var attributeName = "MyStat"; // Get the inventory reference. var inventory = InventorySystemManager.GetInventoryIdentifier(1).Inventory; var itemCollection = inventory.GetItemCollection("MyItemCollection"); var statSum = AttributeUtility.GetFloatSum(attributeName, itemCollection.GetAllItemStacks());
Of course you may wish to compute stats with more advanced functionality. For that 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.
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.GetEquipmentStatInt("MaxHp"); m_Attack = BaseAttack + m_Equipper.GetEquipmentStatInt("Attack"); m_Defense = BaseDefense + m_Equipper.GetEquipmentStatInt("Defense"); } }
The Equipper comes with a useful GetEquipmentStat<Float/Int>(“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<Float/Int>(“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.