How to S/L a list with sub class in ES2

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
tunied
Posts: 11
Joined: Wed Sep 06, 2017 7:27 am

How to S/L a list with sub class in ES2

Post by tunied »

Data Structure

Code: Select all


[Serializable]
public class CreatureVo
{
    public int creatrueType;
    public int blood;

    public override string ToString()
    {
        return string.Format("Creature-> type:{0} blood:{1}", creatrueType, blood);
    }
}

[Serializable]
public class FishVo : CreatureVo
{
        public int color;


        public override string ToString()
        {
            return string.Format("Fish-> type:{0} color:{1} blood:{2}", creatrueType, color, blood);
        }
}

[Serializable]
public class MonkeyVo : CreatureVo
{
    public string name;

    public override string ToString()
    {
        return string.Format("Monkey-> type:{0} name:{1} blood:{2}", creatrueType, name, blood);
    }
}

[Serializable]
public class GameVo
{
    public List<CreatureVo> AllCreatureVoList;

    public GameVo()
    {
        AllCreatureVoList = new List<CreatureVo>();
    }
}


TestCode:

Code: Select all

public class TestLogic : MonoBehaviour
{
    public void OnClickSaveBtnES3()
    {
        var gameVo = GetNewGameVo();
        ES3.DeleteFile("vo");
        ES3.Save<GameVo>("vo", gameVo);
        Debug.Log("ES3 Done save game vo");
    }


    public void OnClickLoadBtnES3()
    {
        var gamevo = ES3.Load<GameVo>("vo");
        gamevo.AllCreatureVoList.ForEach(Debug.Log);

        Debug.Log("ES3 Done load game vo");
    }


    public void OnClickSaveBtnES2()
    {
        var gameVo = GetNewGameVo();
        ES2.Delete("vo");
        ES2.Save(gameVo, "vo");
        Debug.Log("ES2 Done save game vo");
    }

    public void OnClickLoadBtnES2()
    {
        var gamevo = ES2.Load<GameVo>("vo");
        gamevo.AllCreatureVoList.ForEach(Debug.Log);

        Debug.Log("ES2 Done load game vo");
    }


    private GameVo GetNewGameVo()
    {
        var gamevo = new GameVo();

        var monkey = new MonkeyVo
        {
            name = "Monkey",
            blood = 100,
            creatrueType = 1
        };
        gamevo.AllCreatureVoList.Add(monkey);


        var fishVo = new FishVo
        {
            color = 15,
            blood = 70,
            creatrueType = 2
        };
        gamevo.AllCreatureVoList.Add(fishVo);
        return gamevo;
    }
}

Out print log:

ES2:
Creature-> type:1 blood:100
Creature-> type:2 blood:70

ES3:
Monkey-> type:1 name:Monkey blood:100
Fish-> type:2 color:15 blood:70


---

I have test it for ES2 & ES3 . ES3 work fine , but with ES2 it can only store for the parent class.

am i wrong ?


Thanks.
Eran.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: How to S/L a list with sub class in ES2

Post by Joel »

Hi Eran,

In Easy Save 2 inheritance isn't supported in this way by default. However, you can create a custom ES2Type to support it. I describe how to do this in the final post of this thread.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
tunied
Posts: 11
Joined: Wed Sep 06, 2017 7:27 am

Re: How to S/L a list with sub class in ES2

Post by tunied »

Hi Joel

that's really helpful .

thanks :)

Eran.
Locked