I’m not sure that approach would get you where you wanted to go. I have a solution of sorts, but it’s one that will require a bit of modification to the entire Inventory System … (It’s actually not that hard, I’ve done it for several projects).
First, you’ll need to implement ISaveable on InventoryItem, but when you create your CaptureState and RestoreState methods, you’ll make them virtual
public virtual object CaptureState()
{
return null;
}
public virtual void RestoreState(object state)
{
return;
}
This not only sets us up for modifications to ablities, but it also sets us up for things like (what I use them for) random or level based modifiers on equipment.
Each Inventory related CaptureState/RestoreState now has to capture an extra element for each inventory item…
Here’s an example from my Inventory.cs:
[System.Serializable]
public struct InventorySlotRecord
{
public string itemID;
public int number;
public int level;
public object state;
}
public object CaptureState()
{
var slotStrings = new InventorySlotRecord[inventorySize];
for (int i = 0; i < inventorySize; i++)
{
if (slots[i].item != null)
{
slotStrings[i].itemID = slots[i].item.GetItemID();
slotStrings[i].number = slots[i].number;
slotStrings[i].level = slots[i].item.Level;
slotStrings[i].state = slots[i].item.CaptureState();
}
}
return slotStrings;
}
public void RestoreState(object state)
{
var slotStrings = (InventorySlotRecord[])state;
for (int i = 0; i < slotStrings.Length; i++)
{
var item = InventoryItem.GetFromId(slotStrings[i].itemID);
if (item)
{
if (!item.IsStackable())
{
var newItem = Instantiate(item);
item = newItem;
item.RestoreState(slotStrings[i].state);
}
item.Level = slotStrings[i].level;
slots[i].item = item;
slots[i].number = slotStrings[i].number;
}
}
InventoryUpdated?.Invoke();
}
So what’s happening with this is that CaptureState() is using a modified InventorySlotRecord, which stores the item ID, the quantity, the level (in Beneath, the level determines how strong the default stats are) and a state, where bonus attributes are kept.
Any item that is not stackable is instantiated instead of simply referenced. The good news is that when you instantiate an item, it maintains the same ItemID, meaning that since each Basic Sword is it’s own unique instance, it all ties back to the original. All CaptureState has to do is copy the itemID and ask the item for it’s state, which encompass the things that make the item unique.
In RestoreState, if it’s not stackable, then an instance is created, and the information is restored by sending it the state.