Trying to save a class but it always saves null

Discussion and help for Easy Save 3
Post Reply
tomleico
Posts: 2
Joined: Wed Jan 25, 2023 2:54 pm

Trying to save a class but it always saves null

Post by tomleico »

Hi there,

I hope you could help with some trouble I'm having saving a class.
I have a PlayerSave class here:

Code: Select all

public class PlayerSave
{
    // high scores for each level
    public Dictionary<string, float> LevelHighScore { get; set; }

    // dict to hold all unlockables in for the hubroom
    // string will be there name and the bool will be if they are unlocked
    public Dictionary<string, bool> UnlockablesHubroom { get; set; }

    // dict to hold levels unlocked
    // string for name of level
    public Dictionary<string, bool> LevelsUnlocked { get; set; }

    public bool FirstTimePlayer { get; set; }
}
Then I have a method in another script that tries to get the save file if one exists, otherwise I create a new one. See below:

Code: Select all

    
    
    public PlayerSave playerSave = new PlayerSave();
    private string saveKey = "playersave";

    // level codes
    public string[] levelCodes = {"forest", "temple"};
    
    /// <summary>
    /// get save data from file
    /// </summary>
    public void GetPlayerSave()
    {
        // check if there is a save data
        if (ES3.KeyExists(saveKey))
        {
            // data exists so load data
            playerSave = ES3.Load<PlayerSave>(saveKey);
        }
        else
        {
            // data does not exists so lets make some
            playerSave.FirstTimePlayer = true;
            playerSave.LevelHighScore = new Dictionary<string, float>();
            playerSave.LevelsUnlocked = new Dictionary<string, bool>();
            playerSave.UnlockablesHubroom = new Dictionary<string, bool>();
            // add levels with starting score of 0
            foreach(string l in levelCodes)
            {
                playerSave.LevelHighScore.Add(l, 0);
            }
            ES3.Save(saveKey, playerSave);
        }
    }
However, it always seems to save a null class into the data path file. See here:

Code: Select all

{
	"playersave" : {
		"__type" : "PlayerSave,Assembly-CSharp",
		"value" : {
		}
	}
}
So, when I am loading it to the PlayerSave class it just loads in a null.

Would love some insight on this. Been struggling with it the past few days!

Thanks in advance!!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to save a class but it always saves null

Post by Joel »

Hi there, and thanks for getting in contact :)

None of the members of your class are serializable as they are properties rather than fields. Giving each of them the [SerializeField] attribute will make them serializable and resolve your issue. I.e.

Code: Select all

public class PlayerSave
{
    [SerializeField]
    public Dictionary<string, float> LevelHighScore { get; set; }

    [SerializeField]
    public Dictionary<string, bool> UnlockablesHubroom { get; set; }

    [SerializeField]
    public Dictionary<string, bool> LevelsUnlocked { get; set; }

    [SerializeField]
    public bool FirstTimePlayer { get; set; }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tomleico
Posts: 2
Joined: Wed Jan 25, 2023 2:54 pm

Re: Trying to save a class but it always saves null

Post by tomleico »

Yes, that worked! Thank you! I knew the properties had to be public but not serializable.

Also, thanks for the super speedy reply - appreciate it :D
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Trying to save a class but it always saves null

Post by Joel »

Glad to be of assistance, let me know if you run into anything else :)

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