Saving Custom Data: Advanced Method

Early Considerations

For most scenarios, the simple method is more than enough to successfully save custom data. However, there might be situations where you need to save data that cannot be serialized ( excluding Item_SOs as those can be processed by GameDataManager ).

For those situations, you“ll need to expand CustomSaveData to support custom serialization.

Pros
Cons

Supports Custom Serialization

Requires Coding

Saving Custom Data

Imagine you“ve created a script that contains non-serializable data that needs to be saved.

using UnityEngine;
using System.Collections.Generic;

public class Example : MonoBehaviour
{
    // Field that we need to save. Not Serializable
    public Dictionary<string, int> dictionary = new Dictionary<string, int>();
}

In this example scenario, since Dictionary<string, int> cannot be serialized, we“ll need to create a new class that inherits from CustomSaveData, where we can place the data types to be saved. Here“s a step-by-step guide:

1

Include cowsins.SaveLoad & cowsins libraries

using cowsins.SaveLoad;
using cowsins;
2

Ensure your class inherits from Identifiable

Your class needs to inherit from Identifiable ( or any other class that already inherits from Identifiable ) so the Save & Load add-on can effectively load the data.

public class NonSerializableExample : Identifiable {}
3

Expanding Custom SaveData

Dictionaries cannot be serialized & stored into a Json file directly, so instead, we“ll store a string array and an int array separately. For that, we need to create a new class that inherits from CustomSaveData, so it can store our custom arrays that will act as if they were a Dictionary.

[System.Serializable]
public class ExampleSaveData: CustomSaveData
{
    public string[] dictionaryKeys;
    public int[] dictionaryValues;
}
4

Processing & Saving Fields

After our Serializable data is structured, we need to process the data from non-serializable to serializable so it can be saved. In this example, we“ll pass keys to a string array, and values to an int array.

public override CustomSaveData SaveFields()
{
        string[] keys = new string[dictionary.Count];
        int[] values = new int[dictionary.Count];

        dictionary.Keys.CopyTo(keys, 0);
        dictionary.Values.CopyTo(values, 0);
        
        return new ExampleSaveData 
        {
                dictionaryKeys = keys,
                dictionaryValues = values,
                SceneName = SceneManager.GetActiveScene().name
        };
}

In this example, we divide the dictionary into two serializable arrays and return an expanded version of CustomSaveData (named ExampleSaveData in this case) containing these arrays. Additionally, we include the SceneName, which is crucial. To accomplish this, make sure to implement the following library:

using UnityEngine.SceneManagement;
5

Processing & Loading Fields

Loading is essentially the same process as Saving but instead of processing from Non-Serializable to Serializable, we need to convert the serializable data back to their original data types and formats. In this case, we“ll populate the dictionary based on the keys and values arrays.

 public override void LoadFields(object data)
    {
         if(!(data is ExampleSaveData customData)) return;
         
         dictionary.Clear(); // Ensure the dictionary starts empty
    
         // Re-Populate Dictionary
         if (saveData.dictionaryKeys != null && saveData.dictionaryValues != null &&
             saveData.dictionaryKeys.Length == saveData.dictionaryValues.Length)
         {
             for (int i = 0; i < saveData.dictionaryKeys.Length; i++)
             {
                 dictionary[saveData.dictionaryKeys[i]] = saveData.dictionaryValues[i];
             }
         }
        base.LoadFields(data);
    }
6

Notify GameDataManager

At this step of the guide, your CustomSaveData logic is fully set-up, we just need a way to tell the GameDataManager that this class needs to be saved. Luckily for us, this can be done the same way as in the Simple Method.

Simply run the following line whenever you want to notify GameDataManager of a data change ( Whenever any of your saveable fields change )

StoreData();

Here“s the full code for this example:

using System.Collections.Generic;
using cowsins;
using cowsins.SaveLoad;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NonSerializableExample : Identifiable
{
    public Dictionary<string, int> dictionary = new Dictionary<string, int>();

    [System.Serializable]
    public class ExampleSaveData : CustomSaveData
    {
        public string[] dictionaryKeys;
        public int[] dictionaryValues;
    }

    public override CustomSaveData SaveFields()
    {
        string[] keys = new string[dictionary.Count];
        int[] values = new int[dictionary.Count];

        dictionary.Keys.CopyTo(keys, 0);
        dictionary.Values.CopyTo(values, 0);

        return new ExampleSaveData
        {
            dictionaryKeys = keys,
            dictionaryValues = values,
            SceneName = SceneManager.GetActiveScene().name
        };
    }

    public override void LoadFields(object data)
    {
        if (!(data is ExampleSaveData customData)) return;

        dictionary.Clear();

        if(customData.dictionaryKeys != null && customData.dictionaryValues != null && customData.dictionaryKeys.Length == customData.dictionaryValues.Length)
        {
            for(int i = 0; i < customData.dictionaryKeys.Length; i++)
            {
                dictionary[customData.dictionaryKeys[i]] = customData.dictionaryValues[i];
            }
        }

        base.LoadFields(data);
    }

    public override void LoadedState()
    {

    }
}


Checklist

  1. You need to save non-serializable data types from a custom class

  2. You implemented cowsins.SaveLoad library

  3. Your class inherits from Identifiable

  4. You expanded CustomSaveData

  5. You are processing data into Serializable Fields in SaveFields()

  6. You are loading data back to its original format in LoadFields()

  7. You are using StoreData() somewhere in your code to ensure data is sent to the GameDataManager


What to do now? Loaded State.

Once you are done Saving & Loading data, you can run custom behaviour based on the loaded data. Whenever LoadFields is called, LoadedState() will also be called.

Check this guide for more Information.

Last updated