Inventory.GetItemInternal IndexOutOfRangeException

Vesuvian

New member
Hey guys

I'm working my way through giving my new character a pistol weapon. I'm using the online documentation and the demo scene as a reference.

When I run my scene I immediately get the following stack trace:

Code:
IndexOutOfRangeException: Index was outside the bounds of the array.
Opsive.UltimateCharacterController.Inventory.Inventory.GetItemInternal (System.Int32 slotID, Opsive.Shared.Inventory.IItemIdentifier itemIdentifier) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/Inventory.cs:161)
Opsive.UltimateCharacterController.Inventory.Inventory.HasItemInternal (Opsive.UltimateCharacterController.Items.Item item) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/Inventory.cs:64)
Opsive.UltimateCharacterController.Inventory.InventoryBase.HasItem (Opsive.UltimateCharacterController.Items.Item item) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/InventoryBase.cs:136)
Opsive.UltimateCharacterController.Items.Item.Start () (at Assets/Opsive/UltimateCharacterController/Scripts/Items/Item.cs:352)

It comes from the following UCC Inventory method:

C#:
        protected override Item GetItemInternal(int slotID, IItemIdentifier itemIdentifier)
        {
            if (itemIdentifier == null) {
                return null;
            }

            if (m_ItemIdentifierMap[slotID].TryGetValue(itemIdentifier, out var item)) {
                return item;
            }
            return null;
        }

My inventory has a size of 2 (0 - Pistol, 1- Pistol Bullet) but for some reason UCC is checking for the pistol at index (Slot ID) 2, which is outside the bounds of the array. How can I resolve this problem?


As an aside, I'm becoming increasingly frustrated with the number of NullReference and IndexOutOfRange exceptions UCC generates. I previously ran into a problem where a custom movement type did not serialize properly, putting the asset into an unrecoverable state due to null-refs.

Digging through the source code I'm discovering a surprising lack of validation. Null-refs and out-of-bounds are situations that should never make it to the end user of your product.

It's extremely tempting to add null checks and other validation here and there, but I'd hate to lose that effort when upgrading to the next version of UCC. Is there a GIT repository for submitting PRs?


Thanks in advance for any help, and apologies for the belly-aching.
 
Ok, after some more digging, I think the mistake I made was accidentally using the item manager to edit the Third Person Object located on the character rig.

I was able to solve the problem by deleting both the Item and the Third Person Object and starting from scratch.
 
Glad you got it working! For some details:

My inventory has a size of 2 (0 - Pistol, 1- Pistol Bullet) but for some reason UCC is checking for the pistol at index (Slot ID) 2, which is outside the bounds of the array. How can I resolve this problem?
The Slot ID is the location that the item should be equipped (left hand, right hand), which is different from the number of items within the inventory. Your Slot ID should be based on the number of Item Slot components. This video for more info:


With that said, there shouldn't be that exception so I'll add a check. Let me know where else you receive any exceptions.
 
Top