new saved type always returning null

Discussion and help for Easy Save 3
Post Reply
impo
Posts: 4
Joined: Sat Oct 12, 2019 1:08 pm

new saved type always returning null

Post by impo »

Sorry, I feel like this would be a common question and I'm sorry if I'm missing the obvious, but I can't get this to work, even after looking at other similar questions on this forum.

I am trying to save a User class, but on load the reference is always null.
Is there anything else I am missing in this process?

I have a game object with this script attached:

Code: Select all

public class DataManager : MonoBehaviour
{
    public User User { get; set; }

    void Start()
    {
        if (ES3.KeyExists("FirstLoad"))
        {
            print("This is not the first time you have loaded this game");
            User = ES3.Load<User>("User");
            print(User.Username);
        }
        else
        {
            //construct save file
            print("This is the first time this game has been loaded");
            ES3.Save<bool>("FirstLoad", true);
            ES3.Save<User>("User", new User());
        }
    }
}
And the code/constructor for the User is as follows:

Code: Select all

public class User
{
    public int Id { get; }
    public string Username { get; }
    public string Gender { get; }
    public int Steps { get; }
    public Dog Main { get; }

    public User()
    {
        Id = 1;
        Username = "Impo";
        Gender = "Male";
        Steps = 0;
        Main = null;
    }
}
Would anyone know what I am doing wrong? Or if this isn't possible, is there another solution for this?

Thanks for any help
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: new saved type always returning null

Post by Joel »

Hi there,

Please could you PM me a basic project which replicates this, as it's not possible to tell what is happening from what you've posted.

Also could you let me know if any errors are being logged to console?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: new saved type always returning null

Post by Joel »

Thanks for sending the project over. I've managed to locate the issue and this will be fixed in the next update. It looks like there was a quirk where an instance would not be created when loading classes with no serialisable fields.

In the meantime I'll send you over a fix which addresses this.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
impo
Posts: 4
Joined: Sat Oct 12, 2019 1:08 pm

Re: new saved type always returning null

Post by impo »

(sorry, for some reason my messages are stuck in my outbox, so I'm covering all my bases and posting here too)

Thanks Joel,

I have come across another problem if you would be able to help.
I'm trying to save a List of two new Classes: Syncs and Dogs

I've tested this with the newly working User class, and I can make a list from that no problem.
However, the two new classes give me this error:
NotSupportedException: ES3Type.type is null when trying to create an ES3Type for System.Collections.Generic.List`1[Dog], possibly because the element type is not supported.
ES3Internal.ES3TypeMgr.CreateES3Type (System.Type type, System.Boolean throwException) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3TypeMgr.cs:110)
ES3Internal.ES3TypeMgr.GetOrCreateES3Type (System.Type type, System.Boolean throwException) (at Assets/Plugins/Easy Save 3/Scripts/Types/ES3TypeMgr.cs:24)
ES3Writer.Write[T] (System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:86)
ES3.Save[T] (System.String key, System.Object value) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:30)
DataManager.Start () (at Assets/DataManager.cs:41)
Looking at your comment, would this still be the [SerializeField] attribute is not on the properties in the class?
Would just adding a [SerializeField] attribute fix this?

I've attached a small project with the errors, adding on from my first project: removed

Please let me know if you'd be able to help me fix this. Thanks again
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: new saved type always returning null

Post by Joel »

Hi there,

Messages stay in Outbox until the recipient reads them, so this is normal :) Also I deleted the link in your message because this contained the entire Easy Save package, which would allow anyone to download it. In the future if you could PM me files that would be great.

The reason you are getting this error is because your Dog class is not a supportable type, because it does not have a parameterless constructor. For more information on what's supported, please see the Supported Types guide: http://docs.moodkie.com/easy-save-3/es3 ... ted-types/

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
impo
Posts: 4
Joined: Sat Oct 12, 2019 1:08 pm

Re: new saved type always returning null

Post by impo »

Thanks for all your help Joel!

Apologies, I'm used to outbox meaning something that was trying to be sent that failed, hope your inbox wasn't too clustered.

Ah, I see. I've added the parameterless constructors and the error is gone :) - does this mean it is now a supported type?

However, I've noticed when loading the variables after saving, it doesn't load the values I save but loads values from the parameterless constructor.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataManager : MonoBehaviour
{
    public User User { get; set; }
    public List<Dog> Dogs { get; set; }
    public List<Syncs> Syncs { get; set; }

    // Start is called before the first frame update
    void Start()
    {
        if (ES3.KeyExists("FirstLoad"))
        {
            print("This is not the first time you have loaded this game");
            User = ES3.Load<User>("User");
            print(User.ToString());
            print(ES3.Load<List<User>>("UList")[0].Username);
            print(ES3.Load<List<User>>("UList")[1].Username);
            Dogs = ES3.Load<List<Dog>>("Dogs");
            Syncs = ES3.Load<List<Syncs>>("Syncs");

            foreach (Dog dog in Dogs)
            {
                print(dog.ToString());
            }
        }
        else
        {
            //construct save file
            print("This is the first time this game has been loaded");
            ES3.Save<bool>("FirstLoad", true);
            ES3.Save<User>("User", new User());
            Dogs = new List<Dog>();
            Dogs.Add(new Dog(1, "Dog1", "Male", 0, true));
            Dogs.Add(new Dog(2, "Dog2", "Female", 0, true));
            Dogs.Add(new Dog(3, "Dog3", "Female", 20000, false));
            Dogs.Add(new Dog(4, "Dog4", "Male", 30000, false));
            Dogs.Add(new Dog(5, "Dog5", "Male", 50000, false));
            ES3.Save<List<Dog>>("Dogs", Dogs);
            ES3.Save<List<Syncs>>("Syncs", new List<Syncs>());
            List<User> userList = new List<User>();
            userList.Add(new User());
            userList.Add(new User());
            ES3.Save<List<User>>("UList", userList);
        }
    }
}
I've PM'd you another example project. Thank you for any help :)
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: new saved type always returning null

Post by Joel »

Hi there,

Only fields will be serialised by default, not properties. If you want your properties to be serialised, use the [SerializeField] attribute on them.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
impo
Posts: 4
Joined: Sat Oct 12, 2019 1:08 pm

Re: new saved type always returning null

Post by impo »

Thanks again Joel,

I really appreciate your quick replies!
I've changed all of my classes to have fields instead of properties and the lists seem to be saving and loading correctly.

Problem solved! :D
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: new saved type always returning null

Post by Joel »

Glad that's all working for you now :)

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