Page 1 of 1

Loading a custom class doesn't work?

Posted: Mon Jul 11, 2022 7:51 pm
by Mipzy
Hello I tried saving a custom class for my character that contains string:Name, Dictionary<string,float> floats, Dictionary<string,bool> bools, Dictionary<string,string> strings data types.

Code: Select all

public class Character : MonoBehaviour
{
    public string characterName = new string("default");
    public Dictionary<string, float> floatAttributes = new Dictionary<string, float>();
    public Dictionary<string, bool> boolAttributes = new Dictionary<string, bool>();
    public Dictionary<string, string> stringAttributes = new Dictionary<string, string>();

    public void Initialize(string name, Dictionary<string, float> floats, Dictionary<string,bool> bools, Dictionary<string,string> strings)
    {
        characterName = name;
        floatAttributes = floats;
        boolAttributes = bools;
        stringAttributes = strings;
    }

}
Then I tried to test saving the values using:

Code: Select all

Character newChar = new Character();
        newChar.characterName = "Boi";
        newChar.floatAttributes.Add("strength", 15);
        newChar.floatAttributes.Add("intelligence", 10);
        newChar.boolAttributes.Add("isHuman", true);
        newChar.stringAttributes.Add("Catch Phrase", "Deez Nutz");
        
        ES3.Save(newChar.characterName, newChar);
        
Saving didn't produce errors, but when I tried to Load like this:

Code: Select all

Character loadedChar = ES3.Load("Boi", new Character());
loadedChar returns null. Any way to do this correctly?

Re: Loading a custom class doesn't work?

Posted: Tue Jul 12, 2022 9:44 am
by Joel
Hi there,

There is an error in your code unrelated to Easy Save. Your class is a MonoBehaviour and you're using 'new' to create a new instance of it, which is not allowed in Unity. You should be receiving the following warnings in console telling you this:
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
Changing your Character class so that it doesn't inherit from MonoBehaviour resolved the issue.

All the best,
Joel