Custom Item_SOs
Item_SO
can be extended to create various types of items with custom behaviors and abilities triggered upon use. If your item does not require any custom behavior or is not intended to be used, there's no need to extend Item_SO
.
For example, AppleItem_SO
heals the player when used. To implement this functionality, it extends Item_SO
and overrides the Use
method.
Create a Custom Item_SO & Use Behaviour
For this guide, let´s imagine that we´re adding a potion that increases the heal we receive.
Create a New Script
First, we´ll need to create a new script. For organization & clarity purposes, we´ll call this C# script PotionItem_SO
. Since this script will inherit from Item_SO
, it´s highly recommended to follow the naming convention: NameItem_SO
using cowsins;
using UnityEngine;
[CreateAssetMenu(fileName = "newPotionInventoryItem", menuName = "COWSINS/Inventory/Showcase Items/New Potion")]
public class AppleItem_SO : Item_SO
{
public float healMultiplierToAdd = .1f;
}
Implement the Use Method / Behaviour
Now, we´ll need to make a custom Use method, so whenever we use this Item from the Inventory or Fav Pinned Menu, custom behaviour will be run.
For that, we´ll need to override the base Use method.
public override void Use(InventoryProManager inventoryProManager, InventorySlot slot)
{
base.Use(inventoryProManager, slot);
}
Notice how you have access to the InventoryProManager and the InventorySlot from which the Item has been used.
For this specific example, we´ll implement the Heal Multiplier logic:
public override void Use(InventoryProManager inventoryProManager, InventorySlot slot)
{
// Access PlayerMultiplier reference
PlayerMultipliers multiplier = inventoryProManager._WeaponController.GetComponent<PlayerMultipliers>();
// Increases Heal Multiplier
multiplier.healMultiplier += healMultiplierToAdd;
// base.Use ensures the item is consumed.
base.Use(inventoryProManager, slot);
}
Final Script
using cowsins;
using UnityEngine;
[CreateAssetMenu(fileName = "newPotionInventoryItem", menuName = "COWSINS/Inventory/Showcase Items/New Potion")]
public class AppleItem_SO : Item_SO
{
public float healMultiplierToAdd = .1f;
public override void Use(InventoryProManager inventoryProManager, InventorySlot slot)
{
// Access PlayerMultiplier reference
PlayerMultipliers multiplier = inventoryProManager._WeaponController.GetComponent<PlayerMultipliers>();
// Increases Heal Multiplier
multiplier.healMultiplier += healMultiplierToAdd;
// base.Use ensures the item is consumed.
base.Use(inventoryProManager, slot);
}
}
Last updated