Skip to content

Adding a new item

Terence Qu edited this page Feb 28, 2021 · 3 revisions

Registering an item

Figure 1: InventoryManager class Scripts/InventoryManager.cs

void InitItemAndInventory()
{
    var ItemSprites = Resources.LoadAll<Sprite>("Sprites/items");

    ItemDefinitions.Add(new Item(
        "null",
        "Null",
        "Used to represent empty space.",
        0,
        ItemSprites.Single(sprite => sprite.name == "null")
        ));
    ItemDefinitions.Add(new Item(
        "knife", 
        "Hunting Knife", 
        "A sharp blade used for hunting and utility.", 
        1,
        ItemSprites.Single(sprite => sprite.name == "knife")
        ));
    ItemDefinitions.Add(new HealingItem(
        "bandage", 
        "Bandage", 
        "Bandages will give a small HP revovery.", 
        20,
        ItemSprites.Single(sprite => sprite.name == "bandage")
        ));
}

Within Scripts/InventoryManager.cs#InitItemAndInventory(), add a new instance of Item (or something extending Item for advanced functionality), and give it the following properties through the constructor:

  • ID
  • Name
  • Description
  • Max Stack Quantity
  • Sprite Object

The item will now be able to be used within inventories as an ItemStack.

Advanced item functionality

Figure 2: Item class Scripts/Inventory/Item/Item.cs

/// <summary>
/// Use the item in the stack. This method is intended to be overridden.
/// </summary>
/// <param name="stack">Item stack containing item to be used.</param>
/// <param name="character">Character which used the item.</param>
/// <returns>Whether usage was successful.</returns>
public virtual ItemStack OnUsePrimary(ItemStack stack, EntityInventory character)
{
    return null;
}

/// <summary>
/// Use the item in the stack with a secondary action. This method is intended to be overridden.
/// </summary>
/// <param name="stack">Item stack containing item to be used.</param>
/// <param name="character">Character which used the item.</param>
/// <returns>Whether usage was successful.</returns>
public virtual ItemStack OnUseSecondary(ItemStack stack, EntityInventory character)
{
    return null;
}

Creating custom functionality can be done by extending the Scripts/Inventory/Item/Item.cs class, and implementing the OnUsePrimary and OnUseSecondary methods.

  • OnUsePrimary: Left click item in player hotbar
  • OnUseSecondary: Right click item in player hotbar

The subclass that extends off of Item can then be registered within InventoryManager (see Registering an item) and will have custom left click/right click functionality. See the example below.

Figure 2: Example HealingItem, extends from Item Scripts/Inventory/Item/HealingItem.cs

/// <summary>
/// HealingItem is an item, which when used heals the player and consumes itself.
/// </summary>
public class HealingItem : Item
{
    public HealingItem(string Id, string Name, string Description, int MaxStackSize, Sprite Sprite) : base(Id, Name, Description, MaxStackSize, Sprite)
    { }
    
    /// <summary>
    /// Use the one from the stack, and heal the player.
    /// </summary>
    /// <param name="stack"></param>
    /// <param name="character"></param>
    /// <returns></returns>
    public override ItemStack OnUseSecondary(ItemStack stack, EntityInventory character)
    {
        var resultStack = new ItemStack(stack.ItemId, stack.Quantity);

        resultStack.Quantity -= 1;
        resultStack.UpdateEmptyStack();
        Debug.Log("Bandage consumed.");

        return resultStack;
    }
}

SUnity Wiki

Overall

Setup

Item

Clone this wiki locally