Help with custom Type - EasySave 3

Discussion and help for Easy Save 3
Post Reply
basil3
Posts: 2
Joined: Fri Oct 18, 2019 3:52 pm

Help with custom Type - EasySave 3

Post by basil3 »

Hi,

I need a small pointer for using a custom Type in EasySave 3. I have previously used EasySave 2 around 18 months ago using this method but I don't seem to be able to get EasySave 3 to work in the same way.

I have created a custom class to use as my type:

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StartPlotCust
{
    
    public string plotName {get; set;}
    public string cropName {get; set;}
    public bool isPlanted {get; set;}
    public bool startGrowing {get; set;}
    public float harvestTimer {get; set;}
    public int growStage {get; set;}

}
I have added this using the EasySave 3 management window:
ES3Capture.PNG
ES3Capture.PNG (48.21 KiB) Viewed 2110 times
I am using this type with ES3.Save as follows:

Code: Select all


public List<StartPlotCust> startPlotCustList = new List<StartPlotCust> ();

...

foreach (Transform t in startPlots.transform) { 

            startPlotCustList.Add(new StartPlotCust {plotName = t.name, cropName = t.GetChild(0).transform.name, isPlanted = t.GetComponent<GrowController>().isPlanted, startGrowing = t.GetComponent<GrowController>().startGrowing, harvestTimer = t.GetComponent<GrowController>().harvestTimer, growStage = t.GetComponent<GrowController>().growStage});
        }
...

ES3.Save<StartPlotCust>("startPlots", startPlotCustList);
I am using the following code to Load via ES3.Load:

Code: Select all

startPlotCustList = ES3.Load<List<StartPlotCust>>("startPlots"); // Load the list
I get the following error:

InvalidOperationException: Trying to load data of type System.Collections.Generic.List`1[StartPlotCust], but data contained in file is type of StartPlotCust.
ES3Reader.ReadTypeFromHeader[T] () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:273)
ES3Reader.Read[T] (System.String key) (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:160)
ES3.Load[T] (System.String key, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:269)
ES3.Load[T] (System.String key) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:240)


Am I missing something in the way I have added the custom type? From memory and looking back through my old scripts this was how I implemented ES2 in a previous project but I am perhaps missing something really simple...usually the way.

Any pointers would be very much appreciated!

Many thanks,

Stephen.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Help with custom Type - EasySave 3

Post by Joel »

Hi Stephen,

You're using ES3.Save<StartPlotCust>, but startPlotCustList, but is a List<StartPlotCust>. You should use ES3.Save<List<StartPlotCust>>.

Also just a quick note, you don't need to create an ES3Type for your class. You can tell Easy Save to serialise your properties by giving them the [SerializeField] attribute. I.e.
public class StartPlotCust
{
    [SerializeField]
    public string plotName {get; set;}
    [SerializeField]
    public string cropName {get; set;}
    [SerializeField]
    public bool isPlanted {get; set;}
    [SerializeField]
    public bool startGrowing {get; set;}
    [SerializeField]
    public float harvestTimer {get; set;}
    [SerializeField]
    public int growStage {get; set;}

}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
basil3
Posts: 2
Joined: Fri Oct 18, 2019 3:52 pm

Re: Help with custom Type - EasySave 3

Post by basil3 »

J :D Whoops missed that! All working perfectly.

Thank you for quick reply and the heads up about using [SerializeField]... will be a huge help with moving forward.

Stephen.
User avatar
Joel
Moodkie Staff
Posts: 4846
Joined: Wed Nov 07, 2012 10:32 pm

Re: Help with custom Type - EasySave 3

Post by Joel »

Glad that's all working for you now Stephen, let me know if you run into any other problems :)

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