[BUG] Playmaker "Remove Item"

joe1512

New member
Hi, The playmaker "Remove item" script seems to have a bug if the item is categorized as Unique=True, then even if I set remove item to remove 3 for example it will always remove only 1, for now I have set the Category Unique=False and it works as expected and removes the desired amount.

As you also said I could loop the action and use remove item 3 times as a quick fix however having the item as unique=False will work for now. Cheers
 
Setting the item an Unique False does change how the item stacks in the ItemCollection. So it is not allways appropriate to use Mutable and Common. Make sure you understand the difference so that you do not get unexpected results.

Thanks to your issue I found a bug within the UIS main functions, I was potentially removing too many items, or only one if it was unique.

First you can replace this function in the RemoveItem play maker action:

Code:
/// <summary>
/// Removes the item.
/// </summary>
public void DoRemoveItem()
{
    if (m_Inventory == null) { Fsm.Event(m_FailEvent); return; }
    ItemInfo? itemInfoToRemove;
    ItemInfo itemInfoRemoved;
    
    var itemDefinition = m_ItemDefinitionVariable.Value as ItemDefinition;
    if (itemDefinition == null) {
        itemDefinition = m_ItemDefinition;
    }
    var itemCollection = m_Inventory.GetItemCollection(m_ItemCollectionPurpose);
    if (itemCollection != null) {
        itemInfoRemoved = itemCollection.RemoveItem(itemDefinition, m_Amount.Value);
    } else {
        itemInfoRemoved = m_Inventory.RemoveItem(itemDefinition, m_Amount.Value);
    }
    if (itemInfoRemoved.Amount != 0) {
        Fsm.Event(m_SuccessEvent);
    } else {
        Fsm.Event(m_FailEvent);
    }
}


Then in both the Inventory and the ItemCollection script please replace this function (It's the same for both)
Code:
/// <summary>
/// Remove an item from the itemCollection.
/// </summary>
/// <param name="itemDefinition">The item to remove.</param>
/// <param name="amount">The amount to remove.</param>
/// <returns>Returns true if the item was removed correctly.</returns>
public virtual ItemInfo RemoveItem(ItemDefinition itemDefinition, int amount = 1)
{
    if (itemDefinition == null || amount == 0) { return ItemInfo.None; }
    // The item can be in multiple stacks, for example if it is Unique or in a multiItemStackItem.
    var amountRemoved = 0;
    var amountToRemove = amount;
    ItemInfo lastItemInfoRemoved = ItemInfo.None;
    for (int i = 0; i < amount; i++) {
        var itemInfo = GetItemInfo(itemDefinition, false);
        if (itemInfo.HasValue == false) { break; }
        lastItemInfoRemoved = RemoveItem((amountToRemove, itemInfo.Value));
        amountRemoved += lastItemInfoRemoved.Amount;
        amountToRemove = amount - amountRemoved;
        
        if (amountToRemove == 0) { break; }
    }
    
    return new ItemInfo(amountRemoved, lastItemInfoRemoved);
        
  
}

I added a Unit Test for this use case so it should work.
Removing a Unique item with a amount bigger then one will still remove 1 item because only one will have the same ID. but when you'll specify the item definition it will now work as expected.
Thank you for bringing this to my attention
 
Top