Hello,
I am attempting to save/load inventory data that contains items as scriptable objects. I followed a video that works on a basic level, but with static SO items where the stats on a “Bronzesword of Strength” are predetermined and manually input into an Array in the editor. I can generate these, Save and load these. Here is where I have deviated and am trying to adjust the system to my needs.
I have implemented a D2/PoE style of item randomization that gives items stats based on the random Prefixes/Suffixes that are assigned at instantiation. This means that there are many, many more combinations of stats/items and no need to manually populate these in the inspector. But, I cannot seem to modify the existing Save/Load to work with these new SO items. Code below:
Existing Save/Load for Inventory:
private void SaveInventory(SaveData data)
{
List<SlotScript> slots = InventoryScript.instance.GetAllItems();
foreach (SlotScript slot in slots)
{
data.MyInventoryData.MyItems.Add(new ItemData(slot.MyItem.MyTitle, slot.MyItems.Count, slot.MyIndex, slot.MyBag.MyBagIndex));
}
}
private void LoadInventory(SaveData data)
{
foreach(ItemData itemData in data.MyInventoryData.MyItems)
{
Item item = Instantiate(Array.Find(items, x => x.MyTitle == itemData.MyTitle));
//Item itemObj = Instantiate(itemList.Find(x => x.MyTitle == itemData.MyTitle)); //<---Attempt at fix
for (int i = 0; i < itemData.MyStackCount; i++)
{
InventoryScript.instance.PlaceInSpecific(item, itemData.MySlotIndex, itemData.MyBagIndex);
//InventoryScript.instance.PlaceInSpecific(itemObj, itemData.MySlotIndex, itemData.MyBagIndex); //<---Attempt at fix
}
}
}
The collection called items is the Array that works for the old system. I have tried a multitude of things but the one that seems to be the easiest lift and make sense (in my brain) is changing the array of items to a List and then Add/Remove from the list as they get collected/dropped or Saved/Loaded.
I don’t know how to add these SO items to the List (if it is possible, of course).
Here is my InventoryData class:
[Serializable]
public class InventoryData
{
public List<BagData> MyBags { get; set; }
public List<ItemData> MyItems { get; set; }
public InventoryData()
{
MyBags = new List<BagData>();
MyItems = new List<ItemData>();
}
}
So, am i really close but just not doing something properly? Or am i going down a complicated path and need to rework my entire save/load system or even getting my items away from SO?
I would be happy to share more code or explain further if necessary. Appreciate any and all time spent trying to help me.