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!
Saving a collection of structs
Re: Saving a collection of structs
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
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
-
- Posts: 2
- Joined: Wed Feb 17, 2021 9:13 pm
Re: Saving a collection of structs
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:
As before, the list has the correct length, but all the structs in it are uninitialized.
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();
}
}
Re: Saving a collection of structs
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
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