Hey Everyone! I’ve been looking for how to handle inventories for a long time and I think I finally found a method that works involving Scriptable Objects. Wanted to expose it to the community to see if my architecture approach is feasible or maybe if there’s any insights with pros/cons of doing it this way.
What I’ve done to start is created an Inventory Scriptable Object, and then created a PlayerInventory Asset and a MasterInventory Asset from the SO. The Inventory SO simply is a List, where I’ve created another SO called Item. The Item So contains fields for Item Type, Name, Description, etc, something that all items have in common. Using the Item Types, I’ve created 3 more SO’s, Consumable/Weapon/Armor, which all inherit from the Item SO. I’m able to now create a new Consumable/Weapon/Armor Asset and add it to the MasterInventory Asset.
Now when I want to add an item to the Player’s Inventory, I can do a quick lookup by name and simply add the Item, but here’s where I’m running into issues and I’m sure it has to do with the inheritance that I’ve put forth.
In the MasterInventory, I have both a Potion (consumable) and an Iron Sword (weapon), which both inherit from Item. When I use:
private void CreateItem(string name) // We'll pass the weapon Iron Sword here for example
{
Item mstItm = masterInventory.InventoryList.Find(i => i.itemName == name);
playerInventory.InventoryList.Add(mstItm);
}
I’ve debugged the Add(), and it shows all the information for the item when it is added to the PlayerInventory asset, but the problem, when I run a foreach on the PlayerInventory, I cannot target the subclass fields like WeaponDamage. It restricts me to Item class fields. Not sure if VS Intellisense is going crazy or not.
This leads me to want to architect a Master List for each Item Type and thus my dilemma. Is this an okay approach? Am I making it more difficult then it needs to be? Am I doing something fundamentally wrong? Would greatly appreciate any learnings I can get from this.