Inventory

The Inventory component keeps track of the ItemIdentifiers and CharacterItems the character owns. It also has some useful functions adding/removing ItemIdentifier amounts, adding/removing CharacterItems or even equipping/unequiping and dropping items.

At runtime the Inventory Inspector will show all the Items in the Inventory as well as the currently active (equipped) items.

Inspected Fields

Current Inventory

The Current Inventory foldout will list every item that is currently in the inventory. This list will only be populated when the game is running. This list is purely for informational purposes as the list cannot be edited.

Default Loadout

The character will spawn with any items specified within the Default Loadout. ItemTypes can be added and removed by selecting the plus or minus icon on the bottom right of the list.

Auto Equip

Defines what item will be equipped when loading the loadout.

Auto Spawn Destroy Runtime Character Items

This option allow CharacterItems to be dynamically spawned and destroyed (Character Items are pooled per Character) as soon as the ItemIdentifier is added to the Inventory.
The Inventory will know what prefab to spawn by checking the ItemType prefabs.

Auto Remove Character Items

Should Character Items be automatically removed when the item identifier amount reaches 0? (removed Character Items can optionally be destroyed)

Remove All on Death

Should all of the items be removed when the character dies?

Load Default Loadout on Respawn

Should the inventory load the Default Loadout after the character has respawned?

Unequipped State Name

The state to activate while no items are equipped.

API

Item Identifier

Get all the ItemIdentifiers in the Inventory:

List<CharacterItem> items = inventory.GetAllItemIdentifiers();

Get the amount of an item the Inventory has:

int amount = inventory.GetItemIdentifierAmount(itemIdentifier);

Add an ItemIdentifier amount:

int amountAdded = inventory.AddItemIdentifierAmount(itemIdentifier, amount);

Remove an ItemIdentifier amount:

int amountRemoved = inventory.RemoveItemIdentifierAmount(itemIdentifier, amount);

Adjust an ItemIdentifier amount (positive value to add, negative value to remove):

int amountAdjusted = inventory.AdjustItemIdentifierAmount(itemIdentifier);

Character Items

Get the active CharacterItem from the Inventory:

CharacterItem characterItem = inventory.GetActiveCharacterItem(slotID);

Get the CharacterItem defined by its ItemIdentifier from the Inventory:

CharacterItem characterItem = inventory.GetCharacterItem(itemIdentifier, slotID);

Get all the character items in the Inventory:

ReadOnlyList<CharacterItem> characterItems = inventory.GetAllCharacterItems();

Spawn CharacterItem if the inventory does not spawn it automatically:

bool success = inventory.SpawnItemIdentifiersCharacterItem(itemIdentifier, slotID);

Equip/Unequip

Equip and unequip is performed on the Item Set Manager.

Drop

CharacterItems can be dropped with:

GameObject droppedGameObject = DropItemIdentifiers(dropPrefab, dropPosition, dropRotation, dropItemAmounts, forceDrop, remove);

Events

There are a few events that are very useful to keep track of the items being added, removed, equipped, etc.

A CharacterItem will be added:

EventHandler.RegisterEvent<CharacterItem>(m_Character, "OnInventoryWillAddItem", OnWillAddItem);

A CharacterItem was added:

EventHandler.RegisterEvent<CharacterItem>(m_GameObject, "OnInventoryAddItem", OnAddItem);

A CharacterItem is being picked up:

EventHandler.RegisterEvent<IItemIdentifier, int, bool, bool>(m_GameObject, "OnInventoryPickupItemIdentifier", OnPickupItemIdentifier);
EventHandler.RegisterEvent<CharacterItem, int, bool, bool>(m_GameObject, "OnInventoryPickupItem", OnPickupItem);

A CharacterItem is being equipped:

EventHandler.RegisterEvent<CharacterItem, int>(m_GameObject, "OnInventoryEquipItem", OnEquipItem);

A CharacterItem is being unequipped:

EventHandler.RegisterEvent<CharacterItem, int>(m_GameObject, "OnInventoryUnequipItem", OnUnequipItem);

The ItemIdentifierAmount is changing:

EventHandler.RegisterEvent<IItemIdentifier, int, int>(m_Character, "OnInventoryAdjustItemIdentifierAmount", OnAdjustItemIdentifierAmount);

Example Scripts

Equip ItemSet Script

The script below will equip a specific ItemSet from the ItemSetManager. The item must first be picked up before it can be equipped.

using UnityEngine;
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.Character.Abilities.Items;

/// <summary>
/// Equips the specified ItemSet within the Start method.
/// </summary>
public class Equip : MonoBehaviour
{
    [Tooltip("The character that should drop the item.")]
    [SerializeField] protected GameObject m_Character;
    [Tooltip("The index of the groupthat the ItemSet belongs to.")]
    [SerializeField] protected int m_GroupIndex;
    [Tooltip("The index of the ItemSet that should be equipped.")]
    [SerializeField] protected int m_ItemSetIndex;

    /// <summary>
    /// Equips the ItemSet.
    /// </summary>
    public void Start()
    {
        var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
        var equipUnequipAbilities = characterLocomotion.GetAbilities<EquipUnequip>();
        for (int i = 0; i < equipUnequipAbilities.Length; ++i) {
            if (equipUnequipAbilities[i].ItemSetCategoryIndex == m_CategoryIndex) {
                // Starts equipping to the specified ItemSet.
                // ItemSetIndex: The ItemSet to equip/unequip the items to.
                // ForceEquipUnequip: Should the ability be force started? This will stop all abilities that would prevent EquipUnequip from starting.
                // ImmediateEquipUnequip: Should the items be equipped or unequipped immediately?
                equipUnequipAbilities[i].StartEquipUnequip(m_ItemSetIndex, true, false);
                return;
            }
        }
    }
}

Drop Script

The script below will drop an ItemType from the inventory:

using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;

/// <summary>
/// Drops the specified ItemType within the Start method.
/// </summary>
public class Drop: MonoBehaviour
{
    [Tooltip("The character that should drop the item.")]
    [SerializeField] protected GameObject m_Character;
    [Tooltip("The ItemType that the character should drop.")]
    [SerializeField] protected ItemType m_ItemType;

    /// <summary>
    /// Drops the ItemType.
    /// </summary>
    public void Start()
    {
        var inventory = m_Character.GetComponent<InventoryBase>();
        if (inventory == null) {
            return;
        }

        // A single ItemType can exist in multiple slots. Drop all of the items.
        for (int i = 0; i < inventory.SlotCount; ++i) {
            var item = inventory.GetItem(m_ItemType, i);
            if (item != null) {
                // Removes the item from the inventory. The last parameter drops the item.
                // ItemType: The ItemType to remove.
                // SlotID: The ID of the slot.
                // amount: The amount of the ItemIdentnfier that should be removed.
                // Drop: Should the item be dropped when removed?
                inventory.RemoveItem(m_ItemType, i, 1, true); 
            }
        }
    }
}

Item Amounts

The total amount of ammo the active item has can be determined with the following script:

using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items.Actions;

/// <summary>
/// Outputs the amount of ammo the active item has.
/// </summary>
public class OutputItemAmount : MonoBehaviour
{
    [Tooltip("The character that contains the inventory.")]
    [SerializeField] protected GameObject m_Character;

    /// <summary>
    /// Outputs the item amount.
    /// </summary>
    public void Start()
    {
        var inventory = m_Character.GetComponent<InventoryBase>();
        if (inventory == null) {
            return;
        }

        for (int i = 0; i < inventory.SlotCount; ++i) {
            var item = inventory.GetActiveCharacterItem(i);
            if (item != null) {
                Debug.Log("Inventory Amount: " + inventory.GetItemIdentifierAmount(item.ItemIdentifier));
                // A single item can have multiple Item Actions.
                var itemActions = item.ItemActions;
                for (int j = 0; j < itemActions.Length; ++j) {
                    var shootableAction = itemActions[j] as ShootableAction;
                    if (shootableAction != null) {
                        var ammoModuleGroup = shootableAction.AmmoModuleGroup;
                        if (ammoModuleGroup != null) {
                            for (int k = 0; k < ammoModuleGroup.ModuleCount; ++k) {
                                var shootableAmmoModule = ammoModuleGroup.Modules[k] as Items.Actions.Modules.Shootable.ShootableAmmoModule;
                                if (shootableAmmoModule != null) {
                                    Debug.Log("Ammo Remaining: " + shootableAmmoModule.GetAmmoRemainingCount());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}