The focus of the Ultimate Character Controller is to be an exceptional character controller so the inventory system is less focused on features and more focused on being flexible. With that said, in order to have a functioning item system the inventory system does need to be able to support the basics such as keeping an ammo count or determining if the character has a weapon.

The inventory uses ItemTypes to identify a particular item. ItemTypes are also used for consumables such as assault rifle bullets or amount of battery left. ItemTypes can be created within the Item Type Manager.

Pickup Script

The script below will add a new ItemType to the inventory:

using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;

/// <summary>
/// Picks up the specified ItemType within the Start method.
/// </summary>
public class Pickup : MonoBehaviour
{
    [Tooltip("The character that should pickup the item.")]
    [SerializeField] protected GameObject m_Character;
    [Tooltip("The ItemType that the character should pickup.")]
    [SerializeField] protected ItemType m_ItemType;
    [Tooltip("The number of ItemType that the character should pickup.")]
    [SerializeField] protected int m_Count = 1;

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

        // Picks up the ItemType:
        // ItemType: The ItemType to pick up.
        // Count: The amount of ItemType to pick up.
        // -1: The slot ID that picked up the item. A -1 value will indicate no specified slot.
        // true: Should the item be picked up immediately? If false the EquipUnequip ability may not switch to the newly picked up item.
        // false: Should the item be force equipped?
        inventory.PickupItemType(m_ItemType, m_Count, -1, true, false);
    }
}

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(i, m_ItemType);
            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.
                // Drop: Should the item be dropped when removed?
                inventory.RemoveItem(m_ItemType, i, true); 
            }
        }
    }
}

Inspected Fields

Current Inventory

The Current Inventory foldout will list every ItemType 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. New ItemTypes can be added and removed by selecting the plus or minus icon on the bottom right of the list.

Remove All on Death

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

Load Default Loadout on Respawn

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

API

Most for the operations performed directly on the inventory will be to react to an event sent by the inventory rather than performing an operation on the inventory. The inventory exposes many events using the built in Event System:

// Called when the item is initially added to the inventory (ex. an assault rifle has been added to the character at edit time).
OnInventoryAddItem(Item item)

// Called when an ItemType is picked up (ex. the character finds an assault rifle on the ground and picks it up).
OnInventoryPickupItemType(ItemType itemType, float count, bool immediatePickup, bool forceEquip) 

// Called when an Item is picked up. This event is similar to OnInventoryPickupItemType except it is only called for ItemTypes that have an item attached to them.
OnInventoryPickupItem (Item item, float count, bool immediatePickup, bool forceEquip)

// Called when the item is equipped (ex. the event is called for the assault rifle when switching from a knife to an assault rifle).
OnInventoryEquipItem(Item item, int slotID)

// Called when the ItemType is used (ex. the event is called when an assault rifle being fired).
OnInventoryUseItemType(ItemType itemType, float remaining)

// Called when the item is unequipped (ex. the event is called on the assault rifle when switching from an assault rifle to a knife).
OnInventoryUnequipItem(Item item, int slotID)

// Called when the item is removed from the inventory (ex. the event is called when the character dies and drops all of their items).
OnInventoryRemoveItem(Item item, int slotID)

Corresponding Unity events exist for each of these events. The following code can be used as an example for subscribing to one of these events:

using UnityEngine;
using Opsive.UltimateCharacterController.Events;

public class MyObject : MonoBehaviour
{
    public void Awake()
    {
        EventHandler.RegisterEvent<ItemType, int>(gameObject, "OnInventoryUseItemType", OnDamage);
    }

    /// <summary>
    /// The specified ItemType has been used.
    /// </summary>
    /// <param name="itemType">The ItemType that has been used.</param>
    /// <param name="remaining">The remaining amount of the specified ItemType.</param>    
    private void OnUseItemType(ItemType itemType, float count)
    {
        Debug.Log("The inventory used " + itemType.name + ", " + remaining + " is remaining");
    }

    public void OnDestroy()
    {
        EventHandler. UnregisterEvent <ItemType, int>(gameObject, "OnInventoryUseItemType", OnDamage);
    }
}