Singleton load and save scriptable object between scenes

Discussion and help for Easy Save 3
Post Reply
exejutable
Posts: 1
Joined: Wed Mar 27, 2024 3:54 am

Singleton load and save scriptable object between scenes

Post by exejutable »

This is my singleton class DataPersistenceManager in my main scene. When i switch to another scene OtherSceneClass im using as example this class, i load the settings data then do some modifications and call again SaveSettingsFile

Code: Select all

public class DataPersistenceManager: MonoBehaviour
    { 
        
        [SerializeField]
        private SettingsData SettingsData;
       
        public ProfileContainer Profiles;
        
        [Header("Config")]
        
        public string ProfilePath;
        
        public string ProfileID;
        
        private const string settings = "settings";
        
        public static DataPersistenceManager Instance;
        
        public void Awake()
        {
            
            if (Instance != null && Instance != this)
            {
                Destroy(this.gameObject);
            } 
            else
            {
                Instance = this;
                DontDestroyOnLoad(this.gameObject);
                this.LoadActiveSave();
            }
            
          
        }

        private void LoadActiveSave()
        {
            if (this.Profiles.ActiveProfile == null)
            {
                this.Profiles.ActiveProfile = this.Profiles.profiles[0];
               
            }
            
            this.ProfileID = this.Profiles.ActiveProfile.Id;
            this.ProfilePath = Path.Combine(Application.persistentDataPath, this.ProfileID);
            
            this.LoadSavedData();
        }

        private void LoadSavedData()
        {
            if (this.ProfileNoExist())
            {
                this.CreateProfile(); 
            }

            if (this.FileNoExist(settings))
            {;
                this.SaveSettingsFile();
            }

        }
        
        
        private void CreateSettingsData()
        {
            this.SettingsData = SettingsData.CreateInstance();
        }
        
        private void CreateRunData()
        {
            Debug.LogWarning($"{this.name} CreateRunData not implemented");
        }
        
        private void CreateProfile()
        {
            Directory.CreateDirectory(this.ProfilePath);
        }
        
       
        private SettingsData LoadSettings()
        {
            return ES3.Load<SettingsData>(settings, this.GetPath(settings));
        }
        
        private bool ProfileNoExist()
        {
            return !ES3.DirectoryExists(this.ProfileID);
        }

        private bool FileNoExist(string file)
        {
            return !Directory.Exists(this.GetPath(file));
        }

        private string GetPath(string fileName)
        {
            return Path.Combine(this.ProfilePath, $"{fileName}.json");
        }
        
        public void SaveSettingsFile()
        {
            ES3.Save(settings, this.SettingsData, GetPath( settings));
        }

        public SettingsData GetSettings()
        {
            return this.SettingsData;
        }
    }

    class OtherSceneClass: MonoBehaviour
    {
        private void Awake()
        {
            SettingsData settings = DataPersistenceManager.Instance.GetSettings();

            settings.LanguageId = 5;
            settings.IsOnboardingEnabled = true;
            
            
            DataPersistenceManager.Instance.SaveSettingsFile();

        }
    }
    
    
    public class SettingsData: ScriptableObject
    {
        [SerializeField]
        public int LanguageId = 0;

        [SerializeField]
        public bool IsOnboardingEnabled = true;


        public static SettingsData CreateInstance()
        {
            var instance = ScriptableObject.CreateInstance<SettingsData>();

            return instance;
        }
    }
In the second scene return this error
InvalidOperationException: An Easy Save 3 Manager is required to save references. To add one to your scene, exit playmode and go to Tools > Easy Save 3 > Add Manager to Scene. Object being saved by reference is SinglePlayerCCGKit.Scripts.SaveSystem.SettingsData with name SettingsData.
ES3Types.ES3UnityObjectType.WriteObject (System.Object obj, ES3Writer writer, ES3+ReferenceMode mode) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3UnityObjectType.cs:39)
ES3Writer.Write (System.Object value, ES3Types.ES3Type type, ES3+ReferenceMode memberReferenceMode) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:233)
ES3Writer.WriteProperty (System.String name, System.Object value, ES3Types.ES3Type type, ES3+ReferenceMode memberReferenceMode) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:302)
ES3Writer.Write (System.Type type, System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:147)
ES3Writer.Write[T] (System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:134)
ES3.Save[T] (System.String key, T value, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:111)
ES3.Save[T] (System.String key, T value, System.String filePath) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:82)
SinglePlayerCCGKit.Scripts.SaveSystem.DataPersistenceManager.SaveSettingsFile () (at Assets/SinglePlayerCCGKit/Scripts/SaveSystem/DataPersistenceManager.cs:117)
SinglePlayerCCGKit.Scripts.Onboarding.OnBoardingCombat.Start () (at Assets/SinglePlayerCCGKit/Scripts/Onboarding/OnBoardingCombat.cs:95)
I try creating the scriptable object and doing the assignation in the singleton, creating de scriptable object on run time. Also adding the manager to scenes.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Singleton load and save scriptable object between scenes

Post by Joel »

Hi there,

I've had no other reports of this. If you've added the manager to all of your open scenes using the instructions in the error message and you're still getting the error, please could you create a new project with a simple scene which replicates this and send it to me using the form at moodkie.com/contact along with step by step instructions.

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