Loading a custom class doesn't work?

Discussion and help for Easy Save 3
Post Reply
Mipzy
Posts: 1
Joined: Mon Jul 11, 2022 7:44 pm

Loading a custom class doesn't work?

Post 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?
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading a custom class doesn't work?

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply