Problem Clearing Inventory

Zaddo

Active member
I am somehow getting the inventory into a corrupted state. I try to clear it by calling the inventory.RemoveAllItems(), also tried inventory.RemoveAllItems(true, false)

The problem seems to be that m_itemStacks in ItemCollection does not get cleared and so persists an item in the inventory.

Here is a video of my debug session showing the problem. Sorry it is a bit long.

Is there another clear method i can use to fix this? I tried calling inventory.Initialize(true), but then the ItemSlotCollectionView seems to not be connected to the inventory correctly.

 
Last edited:
That's very odd.
The only reason I can think of the item not being removed is if there is a ItemRestriction preventing the remove.
Could you step through the code again, but this time do step inside the ItemCollection.
And step inside the Remove function to see if there isn't something preventing the remove.

Until we find the root of the problem you could clear the itemStacks list here
1668007131392.png

Also could you try replace the RemovItem(itemStackItem, itemStack.Amount)
by
Code:
RemoveItem((ItemInfo)itemStack);
Just to see if that fixes it?
 
I found the problem. The inventory gets corrupted with an item left in m_ItemStacks but not in any slot. This occurs when I try to add a new item into a slot that is occupied. I have made changes to the code so that Items do not automatically stack and for handling inventory in a client/server game. But I don't think my changes are related to this behavior.

Sorry, it is another longish video to show how the inventory gets corrupted in this situation. I probably shouldn't be adding an item into an occupied slot, so this method is probably working as designed. I am using the workaround you suggested above to clear m_ItemStacks, this fixes it for me. Thanks :)

 
Last edited:
Thank you for the detailed video, it really helps.

I never noticed this issue because I usually never have my ItemSlotCollection return overflow ticked on.
The ItemAction that exchanges the items usually takes care of exchanging the items correctly.

I would recommend you set it like this:
1668155771378.png

That being said what you reported is still a bug.

So I'm adding an additional condition to prevent overflowing inside itself.
Code:
/// <summary>
/// Handle Items that do not fit in the Inventory after they are added to an item collection.
/// </summary>
/// <param name="originalItemInfo">The original Item Info that was being added.</param>
/// <param name="itemInfoAdded">The item Info Added.</param>
protected virtual void HandleItemOverflow(ItemInfo originalItemInfo, ref ItemInfo itemInfoAdded)
{
    var rejectedItemInfo = new ItemInfo(originalItemInfo.Amount - itemInfoAdded.Amount, originalItemInfo);
    
    if (m_OverflowOptions.ReturnOverflow) {
        // When the item item overflows from items being added to the same slot, the item shouldn't be returned.
        // Checking that the itemcollection isn't this, ensures the collection isn't corrupted.
        if (originalItemInfo.ItemCollection != null && originalItemInfo.ItemCollection != this) {
            var returnedItemInfo = originalItemInfo.ItemCollection.AddItem(rejectedItemInfo);
            itemInfoAdded = (returnedItemInfo.Amount, itemInfoAdded);
        }
    }

Hopefully that fixes your issues.
 
Thank you. Thats perfect :)

I wasn't familiar with the overflow options on the ItemSlotCollection, so that is also helpful to understand what they do :)
 
Top