Properly serialized class is loaded as null

Discussion and help for Easy Save 3
Post Reply
LaP0573
Posts: 2
Joined: Mon Mar 13, 2023 5:46 pm

Properly serialized class is loaded as null

Post by LaP0573 »

We have an interface (IRelic) implemented by an abstract class (ARelic) itself implemented by lots of classes.

The goal is to save all relics into a List. Here is the list of classes :

Code: Select all

public interface IRelic
    {
        public string Name { get; }
        public string Description { get; }
        public RelicRarity Rarity { get; }
        public Character Character => Character.Any;
    }
    

Code: Select all

[Serializable]
    public abstract class ARelic : IRelic
    {
        public abstract string Name { get; }
        public abstract string Description { get; }
        public abstract RelicRarity Rarity { get; }
    }
Here is one Relic example:

Code: Select all

public class BloodVialRelic : IRelic, IEventHandler<OnPlayerTurnStartedEvent>
    {
        public string Name => "Blood Vial";
        public string Description => "Heal 5 HP at the start of combat.";
        public RelicRarity Rarity => RelicRarity.Common;

        private readonly GlobalEventBusMono eventBus;

        public BloodVialRelic()
        {
            // some code
        }
        
        public void Handle(OnPlayerTurnStartedEvent evt)
        {
           // some code
        }
    }
When I save this relic with

Code: Select all

ES3.Save<ARelic>("Relic", bloodVialRelic)
it works fine (I actually don't specify the ARelic generics type, because the relics are all contained into a List<ARelic>), it's properly saved to the save file :

Code: Select all

"__type" : "ClubSandwich.Relics.BloodVialRelic,ClubSandwich.Relics"
But when I load it with

Code: Select all

ES3.Load<ARelic>("Relic", bloodVialRelic)
it returns null.

I have noticed that if I add a public int field to the BloodVialRelic, it's properly loaded. Any relic that has no public fields does return null when loaded, while any relic with public supported field types by ES3 return the proper value.

What's up with that ? I don't need fields in all of them, and I don't see why loading a class that has no public fields would output null.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Properly serialized class is loaded as null

Post by Joel »

Hi there,

Are you using the latest version of Easy Save? There was such an issue in a previous version but was resolved a while ago.

If this isn't the issue, please could you create a script which I can drop into a new scene in a new project which replicates the issue so I can see what is happening.

I tried replicating it with what you've sent but there is an issue with your code. Your BloodVialRelic class isn't an ARelic, so cannot be serialized as one. When I modified it so that it was an ARelic it appeared to work fine for me. Here is the code I used:

Code: Select all

using System.Collections.Generic;
using UnityEngine;

public class EmptyClassTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var list = new List<ARelic>() { new BloodVialRelic(), new BloodVialRelic() };
        ES3.Save("emptyClass", list);

        foreach (var obj in ES3.Load("emptyClass", new List<ARelic>()))
            Debug.Log(obj == null);
    }
}

public interface IRelic
{
    public string Name { get; }
    public string Description { get; }
}

public abstract class ARelic : IRelic
{
    public abstract string Name { get; }
    public abstract string Description { get; }
}

public class BloodVialRelic : ARelic, IRelic
{
    public override string Name => "Blood Vial";
    public override string Description => "Heal 5 HP at the start of combat.";

    public BloodVialRelic()
    {
        // some code
    }
}
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
LaP0573
Posts: 2
Joined: Mon Mar 13, 2023 5:46 pm

Re: Properly serialized class is loaded as null

Post by LaP0573 »

Thanks for the fast response! That was the issue indeed, I updated to the latest version and it now works! Crazy I didn't think of checking for an update, my version was from November :x

(and yeah a typo slipped into my code snippets, meant to inherit ARelic, not implement IRelic ! Was juggling between the stashed non-working code & the current codebase where ARelic doesn't exist haha)
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Properly serialized class is loaded as null

Post by Joel »

Glad that resolved the issue, let me know if you run into anything else :)

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