Saving a collection of structs

Discussion and help for Easy Save 3
Post Reply
TheFirstBert
Posts: 2
Joined: Wed Feb 17, 2021 9:13 pm

Saving a collection of structs

Post by TheFirstBert »

I have a List of structs (which just consist of an int and a string, to represent a high score).

I am saving this list with:
ES3.Save<List<HighScore>>("highScores", highScores);

And loading it with:
highScores = ES3.Load<List<HighScore>>("highScores", new List<HighScore>());

The list that it loads has the correct length, but all the structs seem to just hold default values. Am I missing something? The documentation doesn't list any other nuances with saving collections or structs. The variables in the struct are accessed through Properties, is that the problem? Any help appreciated!
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a collection of structs

Post by Joel »

Hi there,

Only public fields are serialized by default, not properties. You will need to add a SerializeField attribute to them for them to be serialized.

For more information on what is serialized, please see the Supported Types guide:
https://docs.moodkie.com/easy-save-3/es ... ted-types/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
TheFirstBert
Posts: 2
Joined: Wed Feb 17, 2021 9:13 pm

Re: Saving a collection of structs

Post by TheFirstBert »

Thanks for replying Joel. I must be missing something here, even adding [SerializeField] to the properties gives the same behavior. Here is the struct that I am trying to save a collection of:

Code: Select all

using UnityEngine;

public struct HighScore
{
    public HighScore(string name, int highScore)
    {
        Score = highScore;
        Name = name;
    }

    [SerializeField] public string Name { get; }
    [SerializeField] public int Score { get; }

    public override string ToString()
    {
        return Name + ", " + Score.ToString();
    }
}
As before, the list has the correct length, but all the structs in it are uninitialized.
User avatar
Joel
Moodkie Staff
Posts: 4848
Joined: Wed Nov 07, 2012 10:32 pm

Re: Saving a collection of structs

Post by Joel »

Hi there,

You need to add a set method to your property, otherwise it is not considered a variable as it cannot be assigned to after it's initialised.

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