Code: InventorySystem
Updated InventorySystem
Updated: 4/3/2020
If you update all scripts below, then your Inventory display all items correctly:
The initial version of InventorySystem had the InventoryDisplay use ItemInstance as the key for the dictionary of item Counts. Instead, we want to use item as the dictionary key.
In the entire project, there is one item, and many itemInstances. The ItemInstance is a wrapper class that will allow easy expansion to include additional attributes to our inventory items. So, in that case, if we had good, better, best qualities, then we'd need to decide the best way to display those.
Required Code Changes - Code listed below
Inventory.cs
InventoryDisplay.cs
Slot.cs
Inventory Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//modified from
////https://github.com/Toqozz/blog/blob/master/inventory
[System.Serializable]
public class Inventory : ScriptableObject
{
public UnityEvent onInventoryUpdate = new UnityEvent();
/* Inventory START */
public List<ItemInstance> inventory; // - initialized in inspector
public void OnEnable()
{
if (inventory == null)
{
inventory = new List<ItemInstance>();
}
else
{
inventory.Clear();
}
}
public bool SlotEmpty(int index)
{
if (inventory[index] == null || inventory[index].item == null)
{
Debug.Log(" empty slot");
return true;
}
return false;
}
// Remove an item at an index if one exists at that index.
public bool RemoveItem(ItemInstance item)
{
return (inventory.Remove(item)); //returns true/false
}
// Insert an item, return the index where it was inserted. -1 if error.
public void InsertItem(ItemInstance item)
{
Debug.Log("item added to inventory " + item.item.name);
inventory.Add(item); //add to list
///Broadcast event to notify listeners
if (onInventoryUpdate != null)
{
onInventoryUpdate.Invoke();
}
} //end method
} //end classInventoryDisplay Class
Slot Class
Last updated
Was this helpful?