How to cap the inventory size using the doc pickup script without Ultimate Inventory?

Hello, I saw another thread where someone provided an simple inventory solution by checking the amount of items in the players inventory using UCC Version is 2.1.10 using:
Code:
private bool ValidateWeaponsInInventory()
    {
        var maxAmmount = 2;
        var weaponCount = 0;
        var itemTypes = _itemTypes.ToList();
        var inventory = GetComponent<Inventory>();
        foreach(var item in inventory.GetAllItemTypes())
        {
            var match = itemTypes.Contains(item);
            if(!match)
                continue;

            weaponCount += (int)inventory.GetItemTypeCount(item);
        }
        return weaponCount < maxAmmount;
    }

I want to know how to apply this in UCC Version 2.2.4. I found this solution from another user and I want to know how to apply it to the doc pickup script since it doesn't use "Item."

Doc Pickup script:

Code:
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_Amount= 1;

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

        // Adds the specified amount of the ItemType to the inventory.
        // m_ItemType: The ItemType to pick up.
        // m_Amount: 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.Pickup(m_ItemType, m_Amount, -1, true, false);
    }
}

UCC 2.2+ Solution:

Code:
int count = 0;
List<Item> allItems = inventory.GetAllItems();
Inventory inventory = GetComponent<Inventory>();
foreach (Item item in allItems) {
    if (inventory.GetItemIdentifierAmount(item.ItemIdentifier) > 0) {
        count++;
    }
}
 

Andrew

Moderator
Staff member
When you say "apply it to the doc pickup script", do you mean you want to check how many items of that ItemType the character has before picking it up? If so, you can use Inventory.GetItemIdentifierAmount(itemIdentifier). ItemType is both an ItemDefinition and an ItemIdentifier, so you can use it with this method to check the amount in the inventory directly.
 

Andrew

Moderator
Staff member
So before this line:
inventory.Pickup(m_ItemType, m_Amount, -1, true, false);

You can do:
inventory.GetItemIdentifierAmount(itemIdentifier)

You can simply use the ItemType (m_ItemType) as itemIdentifier (no need to cast). It will return the number of that item in the player's inventory.

One thing worth noting is that if you're checking the amount of a consumable ItemType, i.e. ammo, this will only return the number of that item in the inventory, not including the amount currently loaded in the weapon. The "Item Amounts" example script on this page demonstrates how to get both.
 
Thank you, my problem was I didn't use: using Opsive.UltimateCharacterController.Items; It works now but how can I get the currently equipped item?
 

Andrew

Moderator
Staff member
Inventory.GetActiveItem(slotID) will return the Item of the currently active item in that slot (e.g. 0).
 
Do I have to specify the slot? Or can I just get the active item, because I want to just get the currently active Item regardless of slot.
 

Andrew

Moderator
Staff member
Yes you do, because if you were wielding two items, one in each slot, then both would be considered "active".
 
I think I might be misunderstanding something. Two slots can only be active if you have a weapon in the right and left hand right?
 

Andrew

Moderator
Staff member
Yes, but the character will always has 2 available slots regardless of what's currently being held. You can just check slot 0, which is what most of your items are likely to be in. In the case where the character might be carrying an item in slot 1 but not slot 0, you can easily just check if slot 0 is empty first, and if so check slot 1.
 
Okay now Im trying to drop the weapon but i'm getting this error:
C#:
 error CS7036: There is no argument given that corresponds to the required formal parameter 'drop' of 'InventoryBase.RemoveItem(IItemIdentifier, int, int, bool)'

I'm using the drop from the documentation script:
C#:
inventory.RemoveItem(m_ItemType, 1, true);
 
Looking at it more the whole drop script from the documentation doesn't work. These are the errors:
Code:
error CS1503: Argument 1: cannot convert from 'int' to 'Opsive.Shared.Inventory.IItemIdentifier'

error CS1503: Argument 2: cannot convert from 'Opsive.UltimateCharacterController.Inventory.ItemType' to 'int'

error CS7036: There is no argument given that corresponds to the required formal parameter 'drop' of 'InventoryBase.RemoveItem(IItemIdentifier, int, int,
 

Andrew

Moderator
Staff member
From InventoryBase.cs:
C#:
        /// <summary>
        /// Removes the ItemIdentifier from the inventory.
        /// </summary>
        /// <param name="itemIdentifier">The ItemIdentifier to remove.</param>
        /// <param name="slotID">The ID of the slot.</param>
        /// <param name="amount">The amount of the ItemIdentnfier that should be removed.</param>
        /// <param name="drop">Should the item be dropped when removed?</param>
        public void RemoveItem(IItemIdentifier itemIdentifier, int slotID, int amount, bool drop)
The documentation may be slightly outdated, so make sure your method call passes in parameters correctly as above.
 
Now I get:
Code:
error CS1503: Argument 1: cannot convert from 'Opsive.UltimateCharacterController.Items.Item' to 'Opsive.Shared.Inventory.IItemIdentifier'
 

Andrew

Moderator
Staff member
This is because you're trying to pass in an Item type for the IItemIdentifier argument. Item has the ItemIdentifier property, which returns the IItemIdentifier associated with that item. So you can just pass in myItem.ItemIdentifier as the first argument.
 
Top