Can't seem to reload between runs

Discussion and help for Easy Save 3
Post Reply
furroy
Posts: 7
Joined: Sat Mar 31, 2018 1:46 am

Can't seem to reload between runs

Post by furroy »

I don't know what I'm doing wrong.. I'm trying to store a list or array of data. If I'm running it works fine, I can load and save and the number of toons seems correct. However when I stop playing and restart, on the initial load the list comes back empty every time. I can see the data in the JSON save file is there. This is just the one part of the save, it does the file.Sync() call later on. I tried implementing it as a List<ToonState> and array ToonState[] and it does the same thing either wayy

Code: Select all

        public void LoadFromFile(ES3File file)
        {
            // destroy any child objects, we'll recreate them on map loads
            UnityExtensions.SafeDestroyAllChildren(gameObject);
            if (file.KeyExists("Toons"))
            {
                file.LoadInto<ToonState[]>("Toons", toonStates);  // toonStates loads the array fine until i stop playing and after restart it's always empty
            }
            else
            {
                toonStates = new ToonState[0];
            }
            Refresh();
        }

        public void SaveToFile(ES3File file)
        {
            UpdateToonStates();
            file.Save<ToonState[]>("Toons", toonStates);
        }

Code: Select all

{"Toons":{"__type":"Villain.ToonState[],Assembly-CSharp","value":[{"ToonName":"Burly Guard","IsMale":true,"Position":{"x":31.81852,"y":21.04839,"z":0}},{"ToonName":"Burly Guard","IsMale":true,"Position":{"x":35.71773,"y":25.06328,"z":0}}]},"DayNightGameState":{"__type":"Villain.DayNightGameState,Assembly-CSharp","value":{"IsDaytime":true,"DayOfWeek":0,"HourOfDay":7,"MinuteOfDay":46,"deltaTime":0}}}
furroy
Posts: 7
Joined: Sat Mar 31, 2018 1:46 am

Re: Can't seem to reload between runs

Post by furroy »

ok i guess i figured out a workaround.. if i presave the length of the array, and then preallocate it, and then create empty instances for every element, only then does it load properly. this seems like a lot of undocumented hassle unless i missed some easier way of doing it... this was the exact kind of plumbing i was hoping a serialization library would handle for me. or at the very least give me error messages and not silently fail on each of these conditions.

Code: Select all

        public void LoadFromFile(ES3File file)
        {
            // destroy any child objects, we'll recreate them on map loads
            UnityExtensions.SafeDestroyAllChildren(gameObject);
            if (file.KeyExists("NumToons"))
            {
                int num = file.Load("NumToons", 0);
                toonStates = new ToonState[num];
                for (int i = 0; i < num; ++i) toonStates[i] = new ToonState();
            }
            if (file.KeyExists("Toons"))
            {
                file.LoadInto<ToonState[]>("Toons", toonStates);
            }
            else
            {
                toonStates = new ToonState[0];
            }
            Refresh();
        }
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Can't seem to reload between runs

Post by Joel »

Hi there,

This is not intended behaviour, but I don't appear to be able to replicate this at my end with what you've sent me. Please could you send me a basic project which replicates this?

Also note that the purpose of LoadInto is to not create instances, and load into existing instances instead. If you need it to create instances, use Load instead.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
furroy
Posts: 7
Joined: Sat Mar 31, 2018 1:46 am

Re: Can't seem to reload between runs

Post by furroy »

ok whew that's much better. somehow from the examples inthe documentation i got the impression Load() was only for atomic types and you had to do LoadInto() for anything non-atomic.
Post Reply