Page 1 of 1

ES3 Save Base and Concrete Classes

Posted: Tue Jun 15, 2021 4:24 pm
by RequiemOfTheSun
Is it possible to have ES3 save both a base class and the inheriting class at the same time?

Example
public class Mine : Building

Should I be able to save the Mine and the Building without having to go through each building type and have them include the properties from the base class I want to save? When I try I can't get the base class to save, just the Mine. I'm considering a rewrite so both classes are separate but it's a lot of work untangling things.

Re: ES3 Save Base and Concrete Classes

Posted: Tue Jun 15, 2021 4:44 pm
by Joel
Hi there,

Easy Save indeed stores inheritance, and we don't appear to have any issues with this at our end. To demonstrate:

Code: Select all

using UnityEngine;

public class InheritenceTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        ParentClass obj = new ChildClass();
        obj.myInt = 456;
        ES3.Save("obj", obj);
        Debug.Log(ES3.Load<ParentClass>("obj").myInt);
    }

    public class ParentClass
    {
        public int myInt = 123;
    }

    public class ChildClass : ParentClass
    {
        public string myString = "myStr";
    }
}
This will output 456, indicating that the value has been saved and the new instance correctly created.

All the best,
Joel

Re: ES3 Save Base and Concrete Classes

Posted: Fri Jun 18, 2021 4:35 pm
by RequiemOfTheSun
Is it a problem that I'm using ES3UserType's for both classes?

I've set one up for the base and the derived class and seen how the derived UserType will get reached but not the base. I can see this being useful if I wanted to control each derived class by itself but I'd love for the base to handle all the common properties and then have derived classes just save off the new data they need on top of the base class.

Re: ES3 Save Base and Concrete Classes

Posted: Fri Jun 18, 2021 5:13 pm
by Joel
Hi there,

It should still work regardless of whether you have an ES3Type for each or not. You can try this with the InheritenceTest script I created below, and add an ES3Type for ParentClass and ChildClass. Running it will still return 456 after the ES3Types are created.

All the best,
Joel

Re: ES3 Save Base and Concrete Classes

Posted: Tue Jun 29, 2021 10:32 pm
by RequiemOfTheSun
I've pmed you a copy of my test project showing the issue I'm having. Your example saved the component directly while I'm exclusively saving prefabs with ES3. Perhaps this is a problem? Let me know if I need to rethink how I'm tackling this.

Re: ES3 Save Base and Concrete Classes

Posted: Thu Jul 01, 2021 7:42 am
by Joel
Thanks for sending that over, much appreciated.

It's not saving the 'BaseInt' field because you've created an ES3Type telling it to only save the 'ChildInt' field of the Player class. See the attached screenshot.

All the best,
Joel