ES3 Save Base and Concrete Classes

Discussion and help for Easy Save 3
Post Reply
RequiemOfTheSun
Posts: 14
Joined: Tue Dec 29, 2020 4:14 am

ES3 Save Base and Concrete Classes

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

Re: ES3 Save Base and Concrete Classes

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
RequiemOfTheSun
Posts: 14
Joined: Tue Dec 29, 2020 4:14 am

Re: ES3 Save Base and Concrete Classes

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

Re: ES3 Save Base and Concrete Classes

Post 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
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
RequiemOfTheSun
Posts: 14
Joined: Tue Dec 29, 2020 4:14 am

Re: ES3 Save Base and Concrete Classes

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

Re: ES3 Save Base and Concrete Classes

Post 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
Attachments
Capture.PNG
Capture.PNG (49.61 KiB) Viewed 1012 times
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply