[Bug] ItemCollection.Compare() returns true on first type match even if names don't match.

maaxiim

Member
I have 2 Item Droppers attached to my AI, one to drop a weapon and the other to drop some loot. When the dropper logic fires, it calls `ItemCollection.Compare()` to find the correct item collection to take the drops from. The collections are both of type `Drop`, one is `WeaponDrops` the other `ItemDrops`. Since the check for `m_Name` will not return failure if the names don't match, it goes on to return true on the m_Purpose comparison resulting in both drops coming from ItemDrops. The drops have the correct item prefab but actually results in both drops being a medkit, one that looks like a weapon.

The original method was:
C#:
public bool Compare(ItemCollection itemCollection)
{
    if (itemCollection == null) { return false; }

    if (string.IsNullOrWhiteSpace(m_Name) == false && itemCollection.Name == m_Name) { return true; }

    if (m_Purpose == ItemCollectionPurpose.None) { return false; }

    return m_Purpose == itemCollection.Purpose;
}

I changed it to the following and I'm now seeing drops as expected. I don't know if this will break drop functionality elsewhere as I'm still working my way through to see if it has any unintended side effects.

C#:
public bool Compare(ItemCollection itemCollection)
{
    if (itemCollection == null)
    {
        return false;
    }

    if (m_Purpose == ItemCollectionPurpose.None)
    {
        return false;
    }

    return m_Purpose == itemCollection.Purpose && string.IsNullOrWhiteSpace(m_Name) == false &&
        itemCollection.Name == m_Name;
}
 

Sangemdoko

Moderator
Staff member
Thanks for pointing that out.

I went with a different solution, Compare(item collection) should return true if the name and or the purpose match.
In the inventory component I changed the GetItemCollection function such that it does not return the first match, but instead the best match.
The fix will be available in the next update
 
Top