Thanks, took me a while to figure it out. I had initially tried to do the solutions you had mentioned in the previous thread but i had a hard time getting it right so I tried it this way out of curiosity. i remembered I had purchased Easy save not to long ago on special and decided to give it a try. It made the job a lot easier.
here is the save and load scripts I made:
public class SaveInventory : MonoBehaviour
{
public InventoryPlayerList inventoryPlayerList;
public Inventory maskInventory;
public Inventory mComponentInventory;
public Inventory materialInventory;
private void Awake()
{
maskInventory = inventoryPlayerList.maskInventory;
mComponentInventory = inventoryPlayerList.MComponentInventory;
materialInventory = inventoryPlayerList.materialInventory;
}
public void Save_Inventory()
{
//--------Save inventory size-------
ES3.Save("MaskInventorySize", maskInventory.inventorySize);
ES3.Save("MComponentteInventorySize", mComponentInventory.inventorySize);
ES3.Save("MaterialInventorySize", materialInventory.inventorySize);
//--------Save Slots----------------
SaveInventorySlots();
}
public void SaveInventorySlots()
{
var maskSlotStrings = maskInventory.PrepareInventorySlotsToSave();
ES3.Save("MaskInventorySlotRecord", maskSlotStrings);
var mComponnentSlotStrings = mComponentInventory.PrepareInventorySlotsToSave();
ES3.Save("MComponentInventorySlotRecord", mComponnentSlotStrings);
var MaterialSlotStrings = materialInventory.PrepareInventorySlotsToSave();
ES3.Save("MaterialInventorySlotRecord", MaterialSlotStrings);
}
}
public class LoadInventory : MonoBehaviour
{
public InventoryPlayerList inventoryPlayerList;
public Inventory maskInventory;
public Inventory mComponentInventory;
public Inventory materialInventory;
private void Awake()
{
maskInventory = inventoryPlayerList.maskInventory;
mComponentInventory = inventoryPlayerList.MComponentInventory;
materialInventory = inventoryPlayerList.materialInventory;
}
public void Load_Inventory()
{
//-------Load inventory size-------
maskInventory.inventorySize = ES3.Load<int>("MaskInventorySize");
mComponentInventory.inventorySize = ES3.Load<int>("MComponentteInventorySize");
materialInventory.inventorySize = ES3.Load<int>("MaterialInventorySize");
//-------Load inventory slots record------
LoadInventorySlots();
//-------Updateinventory----------
maskInventory.InventoryUpdated();
mComponentInventory.InventoryUpdated();
materialInventory.InventoryUpdated();
}
public void LoadInventorySlots()
{
maskInventory.PrepareInventoryToLoad();
mComponentInventory.PrepareInventorySlotsToSave();
materialInventory.PrepareInventorySlotsToSave();
}
}
Here is what i did in the inventory script for saving and loading:
public object PrepareInventorySlotsToSave()
{
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;
}
}
return slotStrings;
}
public void PrepareInventoryToLoad()
{
// Load after after startup: only mask inventory loads. Maybe I have to save and load the item categories on the items as well for it to load properly
//var slotStrings = new InventorySlotRecord[inventorySize];
if (itemCategory == ItemCategory.Mask)
{
inventorySize = ES3.Load<int>("MaskInventorySize");
slots = new InventorySlot[inventorySize];
Debug.Log("mask inventory size loaded");
//----Load slot test----
var slotStrings = new InventorySlotRecord[inventorySize];
slotStrings = ES3.Load("MaskInventorySlotRecord", slotStrings);
for (int i = 0; i < inventorySize; i++)
{
slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
slots[i].number = slotStrings[i].number;
}
//if (inventoryUpdated != null)
//{
// inventoryUpdated();
//}
Debug.Log("Mask slots loaded");
}
if (itemCategory == ItemCategory.MComponent)
{
inventorySize = ES3.Load<int>("MComponentteInventorySize");
slots = new InventorySlot[inventorySize];
Debug.Log("Mcomponent inventory size loaded");
//----Load slot test----
var slotStrings = new InventorySlotRecord[inventorySize];
slotStrings = ES3.Load("MComponentInventorySlotRecord", slotStrings);
for (int i = 0; i < inventorySize; i++)
{
slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
slots[i].number = slotStrings[i].number;
}
//if (inventoryUpdated != null)
//{
// inventoryUpdated();
//}
Debug.Log("Mcomponent slots loaded");
}
if (itemCategory == ItemCategory.Material)
{
inventorySize = ES3.Load<int>("MaterialInventorySize");
slots = new InventorySlot[inventorySize];
Debug.Log("Marerial inventory size loaded");
}
if (inventoryUpdated != null)
{
inventoryUpdated();
}
}
The only issue I have with the save/Load is If I load the inventory’s after the game has started with a clickable UI button it will only load the Mask inventory. I may have to look into it a some point but I am happy that it loads correctly on start up for now. I put a bool in the inventory scripts to choose in the inspector if i want to load on startup or not. In the future I may come back to try to optimize it if I can,
Here’s a quick look at how I have i have it set up. Each bubble is a script.