Possible Bug: Orphaned character item created when two instances of same item are in equippable collection

Zaddo

Active member
I am using UIS with UCC. If I have two of the same item on my hotbar, both are then in the equippable collection. This will create two of the same character item game objects. Then if one of these items is removed, either by consuming it or dropping it, neither of the character objects is removed. If the remaining item is then removed, it will remove one of the character items. This leaves an orphaned character item under the Items game object on the character.

There are a couple things to mention about my setup. Firstly, I have made UIS multiplayer and so many of the transactions are passed through the networking code and not directly called from the UI, I don't think this is my bug but worth mentioning in case it is :). Secondly, this is only an issue for Mutable/Non Unique items. It is not an issue for Mutable/Unique Items. I have Mutable/Non unique items for stackable consumables so that I can assign attributes for things like Durability and Entity ID.

The problem occurs in InventoryBase.RemoveItemIdentifierAmountInternal. The code that should remove the character item, doesn't because the variables slotID is -1 and newAmount is a positive amount due to the second item in the collection. See the screen capture. The SlotID is ignored when OnRemoveItemInventory calls RemoveItemIdentifierAmountInternal. And the amount is positive because when GetItemIdentifierAmount is called, all similar item quantities are accumulated.

The second screen capture shows my debugging notes for the call stack that leads to this.

If this is fixable, could you please provide a code fix?

UCC Version: 3.1.3
UIS Version: 1.2.22
Unity 6: 6000.0.32f1 (URP)


1738048855990.png

1738048911753.png
 
Last edited:
Ah that's a tricky one...
In the integration we mostly assume items are mutable and Unique. The issue with non unique items is that we cannot differentiate the item via the itemIdentifier because they are the same reference.

The expected result with mutable/common items in the integration is having a single character item. When the player runs out of the item it gets removed.

So the question isn't so much why is the item not being removed. It's why is there a duplicate character item.

In your case do you need multiple character items, one for each amount?
 
I don't need multiple character items, if it were possible to make a change so that only one Character Item was created for both stacks, that would be perfect. I tried doing this myself by changing CharacterInventoryBridge.GetActiveCharacterItemInternal so that for Mutable/Non Unique items it loops through m_ItemToCharacterItemMap and matches on ItemDefinition and returns this character item. This stops the second character item from getting created. But then the second item won't equip. This path is getting too complicated for me to resolve.

Instead, I added some extra logic to remove the character item in CharacterInventoryBridge.OnRemoveItemFromInventory. After RemoveItemIdentifierAmountInternal is called, I call the fix method below. This forces the removal of the Character Item that is linked with the hotbar slot that got consumed/dropped. I had to do this in the bridge and not InventoryBase because we need to use the UIS ItemDefinition class. I haven't been able to break this with my testing. Can you think of any scenarios where this will cause a problem?


C#:
        private void FixOrphanedCharacterItems(ItemInfo itemInfo)
        {
            var itemDef = itemInfo.Item.GetItemDefinition() as ItemDefinition;
            if (itemDef == null || !itemDef.IsMutable || itemDef.IsUnique) return;

            if (TryGetCharacterItem(itemInfo.Item, 0, out var characterItem))
            {
                RemoveCharacterItem(characterItem, m_AutoSpawnDestroyRuntimeCharacterItems, false);
            }
        }
 
Last edited:
I'm not clear on something you mentioned.
But then the second item won't equip
Are you equipping both items in paralel? For that you would need multiple character items.

You mentioned the items are non-unique so they can stack. So when you equip, it should equip one item with a count of two right?


On another note, if your code snippet works and you haven't found issues with it, continue using it.
I would just say that if you have multiple slots then make sure to loop over all of them. Currently you are just looking a slot "0"
 
Are you equipping both items in paralel? For that you would need multiple character items
No I don't equip both in parallel.

What I did was modify the code so that if multiple instances of the item were added to the hotbar, only the first instance would create a character item. The second, third, and so on would not. The first item added to the hotbar worked as expected in Slot 0, but when I tried to equip the others in slot 0, they wouldn’t equip. So, I rolled back this change.

You mentioned the items are non-unique so they can stack. So when you equip, it should equip one item with a count of two right?
Yes, that’s correct. The items can be a stack, such as bandages, food, or throwable pebbles. When the player uses one, it is consumed individually, reducing the stack count by one.

Currently you are just looking at slot "0"
I understand that I should loop over all slots. But I only use Slot 0 in my game. So I was a bit lazy with the code :)

Thanks for getting back to me. While it would be nice if UIS handled this scenario, at least I know I am on the right track with the fix.
 
Back
Top