Load Ocasionally Fails When Restarting Unity

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Load Ocasionally Fails When Restarting Unity

Post by jspro »

Hello Toni,

I've run into a very frustrating bug; I'm not quite sure what the cause of it is.

Here is how I reproduce the bug,
  1. I save the game while testing it.
  2. As long as I don't restart Unity, loading the game works fine. (To be honest, I'm not 100% sure this is true, but it hasn't happened yet. )
  3. I restart Unity, enter play mode, and load the game until the load fails. This usually takes between 1-10 restarts.
This is the saver data class where I first noticed the bug:

Code: Select all

    /// <summary>
    /// Saves the knowledge the player has learned.  
    /// </summary>
    public class InkStateManagerSaver : Saver
    {
        [Serializable]
        public class InkStateManagerData
        {
            public Character talkingTo;
            public List<Presentable> allKnown;
            public List<Presentable> allPresentableRepliesKeys;
            public List<Presentable> allPresentableRepliesValues;
        }
I eventually found that here, while applying the data, when the bug does occurs, the fields of data were null; talkingTo would be null, and the elements of the lists were all null as well:

Code: Select all

            if (string.IsNullOrEmpty(s)) return;
            var data = SaveSystem.Deserialize<InkStateManagerData>(s);
            if (data == null) return;
The save file seems to be fine; after all, the load doesn't always fail. And it's not just this class that's affected. I believe only fields are hold references are affected (although, again, not 100% sure. )

Have you seen anything like this before?

PS: Forgot to mention that I’m using the EasySave3 integration.

Best,
Malek
Last edited by jspro on Tue Jul 26, 2022 8:20 am, edited 1 time in total.
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Load Ocasionally Fails When Restarting Unity

Post by Tony Li »

Hi,

What are Character and Presentable? Are they UnityObject types, such as ScriptableObjects or MonoBehaviours? If so, Unity's serializer can't serialize their references in a way that's consistent between runs. In general, only use basic types in your save data structures. For example, maintain a separate CharacterList:

Code: Select all

public class CharacterList : ScriptableObject
{
    public List<Character> characters;
}
and then store the character's name or index into CharacterList:

Code: Select all

[Serializable]
public class InkStateManagerData
{
    public int characterTalkingTo; // Index into CharacterList's characters list.
    ...

public override string RecordData()
{
    var data = new InkStateManagerData();
    data.characterTalkingTo = MyReferenceToCharacterList.characters.IndexOf(characterThatIAmTalkingTo);
    ...

public override void ApplyData(string s)
{
    var data = SaveSystem.Deserialize<InkStateManagerData>(s);
    characterThatIAmTalkingTo = (0 <= data.characterTalkingTo && data.characterTalkingTo < MyReferenceToCharacterList.Count)
        ? MyReferenceToCharacterList.characters[data.characterTalkingTo] : null;
    ...
Or you could switch to a different serialization method, such as making a custom DataSerializer class that uses Json.NET instead of the save system's JsonDataSerializer. (JsonDataSerializer uses Unity's very fast but limited JsonUtility.)
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Load Ocasionally Fails When Restarting Unity

Post by jspro »

Right, I forgot to mention in my post that I’m using the EasySave3 integration, so the EasySave Serializer should be the one being used, and that asset can serialize Scriptable Objects.
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Load Ocasionally Fails When Restarting Unity

Post by Tony Li »

If you're using ESSavedGameDataStorer, this just sends already-serialized data to ES3. Presumably the data has been serialized with JsonDataSerializer, which is the problem.

If that's the case, you can write a DataSerializer subclass that uses ES3's serialization instead. Make sure to remove JsonDataSerializer from your SaveSystem GameObject and add your custom ES3 DataSerializer in its place.
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Load Ocasionally Fails When Restarting Unity

Post by jspro »

Ahh, that’s probably the culprit then. I’ll give it a shot and update you. Thanks Tony!
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Load Ocasionally Fails When Restarting Unity

Post by Tony Li »

Glad to help!
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Load Ocasionally Fails When Restarting Unity

Post by jspro »

Seems to be all good. Thanks again!
Post Reply