Saving List of scriptable objects with ES3 doesn't load properly in another scene

Discussion and help for Easy Save 3
Post Reply
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Saving List of scriptable objects with ES3 doesn't load properly in another scene

Post by Marcus79 »

Hi guys!

I'm trying to save a list of scriptable objects that are items in a player's inventory. With ES3 this works fine if it's within in the same scene (I tested this within the update loop as you can see in the script). But as soon as I go to the next scene, the list is not null but empty. I made the player inventory a singleton to see if it had to do something with different instance ID's. I did some research and found that this could be an issue, but that didn't seem to help either, so I'll probably change it back.

This is my code:

Code: Select all

using UnityEngine;
using System.Collections.Generic;


namespace Munnica.THL.Inventories
{
    public class PlayerInventory : MonoBehaviour
    {
        public static PlayerInventory Instance;

        [SerializeField] private InventoryObject inventoryObject;


        private void Awake()
        {
            if (Instance == null)
            {
                Instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
                                   
        }

        

        private void LoadInventory()
        {
            if (ES3.KeyExists("Inventory"))
            {
                inventoryObject.container = (List<InventorySlot>)ES3.Load("Inventory");
                print("Loaded Player Inventory");
            }
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.PageDown)) { SaveInventory(); }
            else if (Input.GetKeyDown(KeyCode.PageUp)) { LoadInventory(); }
            else if (Input.GetKeyDown(KeyCode.Delete)) { inventoryObject.container.Clear(); }          

        }

        public void AddItem(InventoryItem item, int quantity)
        {           

            for (int i = 0; i < inventoryObject.container.Count; i++)
            {
                if (inventoryObject.container[i].item == item)
                {
                    inventoryObject.container[i].AddAmount(quantity);

                    
                    break;
                }
            }

            inventoryObject.container.Add(new InventorySlot(item, quantity));
            
        }

        public InventoryObject GetInventory()
        {
            return inventoryObject;
        }
        

        public void SaveInventory()
        {
            ES3.Save("Inventory", inventoryObject.container);
            print("Player Inventory saved!");
        }

        

        
    }
}


Like I said, If LoadInventory() gets called the container (list that hold the scriptable objects) becomes empty;
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of scriptable objects with ES3 doesn't load properly in another scene

Post by Joel »

Hi there,

Have you done a Debug.Log call to check that the List is not empty immediately before you save? Also just to clarify: is the List empty, or is it filled with null values? And are you getting any warnings to console? (ensure that warnings are enabled).

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Marcus79
Posts: 7
Joined: Thu Jun 01, 2023 9:01 am

Re: Saving List of scriptable objects with ES3 doesn't load properly in another scene

Post by Marcus79 »

I get no errors, that makes it so hard to debug. The container is indeed populated. In the other scene it basically defaults to 'None', like inventory item, sprite icon etc. Like I said it works great in a single scene, the problem arises when I go to any other scene.

This is the inventory object script if that helps:

Code: Select all

using System.Collections.Generic;
using UnityEngine;


namespace Munnica.THL.Inventories
{
    [CreateAssetMenu(fileName = "Inventory", menuName = "Game/Inventory System/New Inventory")]
    public class InventoryObject : ScriptableObject
    {
        public List<InventorySlot> container = new List<InventorySlot>();

    }

    [System.Serializable]
    public class InventorySlot
    {
        public InventoryItem item;
        public int quantity;
        public Sprite icon;

        public InventorySlot(InventoryItem item, int quantity)
        {
            this.item = item;
            this.quantity = quantity;
            icon = item.itemIcon;
        }

        public void AddAmount(int value)
        {
            quantity += value;
        }


    }
}


User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving List of scriptable objects with ES3 doesn't load properly in another scene

Post by Joel »

Hi there,

Please could you replicate this in a new project with a simple scene and private message it to me with instructions.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply