[Bug] Some bugs found in versions 1.2.13 and 1.2.12.

WeiYiHua

Member
Recently I found some bugs while reading the source code, please fix them, thanks!

1. UltimateInventorySystem\Editor\VisualElements\ControlTypes\ItemAmountsControl.cs
line: 121
Code:
/// <summary>
/// Remove the element at the index.
/// </summary>
/// <param name="index">The index to remove at.</param>
protected override void Remove(int index)
{
    if (OnRemoveItem != null) {
        if (index < 0 || index >= m_ObjectAmounts.Count) {
            return;
        }
        var itemAmount = m_ObjectAmounts[index];
        OnRemoveItem?.Invoke(itemAmount,  m_ReorderableList.ItemsSource.Count);
    } else {
        base.Remove(index);
    }
    m_ItemFieldContainer.Clear();
}
fix:
Code:
/// <summary>
/// Remove the element at the index.
/// </summary>
/// <param name="index">The index to remove at.</param>
protected override void Remove(int index)
{
    if (OnRemoveItem != null) {
        if (index < 0 || index >= m_ObjectAmounts.Count) {
            return;
        }
        var itemAmount = m_ObjectAmounts[index];
        OnRemoveItem?.Invoke(itemAmount, index);
    } else {
        base.Remove(index);
    }
    m_ItemFieldContainer.Clear();
}

2. UltimateInventorySystem\Editor\VisualElements\ItemCollectionField.cs
Code:
if (Application.isPlaying) {
    m_ItemAmountsView.OnAddItem += (item, index) =>
    {
        m_ItemCollection.AddItem(item);
    };
    m_ItemAmountsView.OnRemoveItem += (itemAmount, index) =>
    {
        m_ItemCollection.RemoveItem((ItemInfo)itemAmount);
    };
    m_ItemAmountsView.OnChange += (index, previousItemAmount, newItemAmount) =>
    {
        var allItemStack = m_ItemCollection.GetAllItemStacks();
        if (index <= 0 || index >= allItemStack.Count) {
            return;
        }
           
        //get that index in the item stacks at runtime
        var itemStackToEdit = allItemStack[index];

        // This can happen id an Item was added or removed.
        if (itemStackToEdit.Item != newItemAmount.Item) {
            return;
        }
           
        var difference = newItemAmount.Amount - itemStackToEdit.Amount;

        if (difference < 0) {
            m_ItemCollection.RemoveItem(new ItemInfo(-difference, itemStackToEdit));
        }else if (difference > 0) {
            m_ItemCollection.AddItem(new ItemInfo(difference, itemStackToEdit));
        }
    };
}
fix:
Code:
if (Application.isPlaying) {
    m_ItemAmountsView.OnAddItem += (item, index) =>
    {
        m_ItemCollection.AddItem(item);
    };
    m_ItemAmountsView.OnRemoveItem += (itemAmount, index) =>
    {
        var allItemStack = m_ItemCollection.GetAllItemStacks();
        if (index < 0 || index >= allItemStack.Count)
        {
            return;
        }

        //get that index in the item stacks at runtime
        var itemStackToEdit = allItemStack[index];

        // This can happen id an Item was added or removed.
        if (itemStackToEdit.Item != itemAmount.Item)
        {
            return;
        }

        m_ItemCollection.RemoveItem((ItemInfo)itemStackToEdit);
    };
    m_ItemAmountsView.OnChange += (index, previousItemAmount, newItemAmount) =>
    {
        var allItemStack = m_ItemCollection.GetAllItemStacks();
        if (index < 0 || index >= allItemStack.Count) {
            return;
        }
           
        //get that index in the item stacks at runtime
        var itemStackToEdit = allItemStack[index];

        // This can happen id an Item was added or removed.
        if (itemStackToEdit.Item != newItemAmount.Item) {
            return;
        }
           
        var difference = newItemAmount.Amount - itemStackToEdit.Amount;

        if (difference < 0) {
            m_ItemCollection.RemoveItem(new ItemInfo(-difference, itemStackToEdit));
        }else if (difference > 0) {
            m_ItemCollection.AddItem(new ItemInfo(difference, itemStackToEdit), itemStackToEdit);
        }
    };
}
 
Last edited:
3.UltimateInventorySystem\Editor\Managers\UIDesigner\UIDesignerSchema.cs
line: 400

Code:
public override (bool isValid, string message) ValidateField(string fieldName, GameObject gameObject)
{
    var result = base.ValidateField(fieldName, gameObject);

    var valid = result.isValid;
    var message = result.message;

    var foundGameplayPanel = false;
    var foundMainMenu = false;

    for (int i = 0; i < gameObject.transform.childCount; i++) {
        var child = gameObject.transform.GetChild(i);
fix:
Code:
public override (bool isValid, string message) ValidateField(string fieldName, GameObject gameObject)
{
    var result = base.ValidateField(fieldName, gameObject);

    var valid = result.isValid;
    var message = result.message;

    if (gameObject == null)
    {
        return (valid, message);
    }

    var foundGameplayPanel = false;
    var foundMainMenu = false;

    for (int i = 0; i < gameObject.transform.childCount; i++) {
        var child = gameObject.transform.GetChild(i);
 
4. UltimateInventorySystem\Editor\Managers\InventoryMainWindow.cs
line: 208
Code:
/// <summary>
/// The window has been enabled.
/// </summary>
protected override void OnEnable()
{
    s_Instance = this;
    rootVisualElement.styleSheets.Add(CommonStyles.StyleSheet);
    rootVisualElement.styleSheets.Add(ControlTypeStyles.StyleSheet);

    // Initialize the database.
    if (Application.isPlaying) {
        m_Database = FindObjectOfType<InventorySystemManager>().Database;
    } else  if (!string.IsNullOrEmpty(DatabaseGUID)) {
        m_Database = Shared.Editor.Utility.EditorUtility.LoadAsset<InventorySystemDatabase>(DatabaseGUID);
    }

InventorySystemManager probably doesn't exist, suggest a null check for this or something similar.
 
5. UltimateInventorySystem\Scripts\UI\Item\ItemViewSlotsContainerBase.cs
line: 332
Code:
/// <summary>
/// Set the inventory.
/// </summary>
/// <param name="inventory">The inventory.</param>
public virtual void SetInventory(Inventory inventory, bool handleChange)
{
    if (m_Inventory == inventory && m_IsInventorySet) { return; }

    m_IsInventorySet = true;

    UnregisterFromInventoryUpdate();

    var previousInventory = inventory;
    m_Inventory = inventory;
    OnInventoryChanged(previousInventory, m_Inventory);

    RegisterToInventoryUpdate();

    if (handleChange) {
        HandleInventoryUpdate();
    }
}

fix:
Code:
/// <summary>
/// Set the inventory.
/// </summary>
/// <param name="inventory">The inventory.</param>
public virtual void SetInventory(Inventory inventory, bool handleChange)
{
    if (m_Inventory == inventory && m_IsInventorySet) { return; }

    m_IsInventorySet = true;

    UnregisterFromInventoryUpdate();

    var previousInventory = m_Inventory;
    m_Inventory = inventory;
    OnInventoryChanged(previousInventory, m_Inventory);

    RegisterToInventoryUpdate();

    if (handleChange) {
        HandleInventoryUpdate();
    }
}
 
6. UltimateInventorySystem\Scripts\Core\InventoryCollections\ItemCollectionGroup.cs
line: 141
Code:
for (int i = 0; i < itemCollectionNames.Count; i++) {
    var itemCollection = inventory.GetItemCollection(itemCollectionNames[i]);
    if (itemCollection == null) {
        if (logWarningIfMissing) {
            Debug.LogWarning($"The Item Collection with the name '{m_ItemCollections[i]}' does not exist in the inventory.")
        }
        continue;
    }
    itemCollections.Add(itemCollection);
}
fix:
Code:
for (int i = 0; i < itemCollectionNames.Count; i++) {
    var itemCollection = inventory.GetItemCollection(itemCollectionNames[i]);
    if (itemCollection == null) {
        if (logWarningIfMissing) {
            Debug.LogWarning($"The Item Collection with the name '{itemCollectionNames[i]}' does not exist in the inventory.");
        }
        continue;
    }
    itemCollections.Add(itemCollection);
}
 
7. UltimateInventorySystem\Scripts\DropsAndPickups\ItemAmountProbabilityTable.cs
line: 63
UltimateInventorySystem\Scripts\DropsAndPickups\ItemInfoProbabilityTable.cs
line: 45
line: 63
line: 81
Code:
if (item.IsMutable) { item = Item.Create(item); }

The Items created in the above locations do not seem to be used.
 
There is another bug.

UltimateInventorySystem\Scripts\UI\Panels\ItemViewSlotContainers\ItemViewSlotPanelToTooltip.cs

The PlacePanelInternal method fixes the bug when RenderMode.ScreenSpaceCamera,
But the AdjustPositionIfOutOfBounds method does not.
 
Thank you so much for the detailed bug report!
I took note and hopefully we'll get them all fixed by the next update
 
Top