Save nested ScriptableObjects

Discussion and help for Easy Save 3
Post Reply
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Save nested ScriptableObjects

Post by Rafarel »

Hello there,

I have two issues with my saving system, hope you can help me on this :)

# ISSUE 1

I have a PlayerData ScriptableObject that contains other nested ScriptableObjects.
I can't save the children, ES3 just saves me the first one and the nested ScriptableObjects are not saved.

Here are my objects :

Code: Select all

public class PlayerData : ScriptableObject
{
    public string nickname = "player";
    public int score;
    public string noSave;

    public ItemData[] items;
    public WorldsData worlds;
}

Code: Select all

public class ItemData : ScriptableObject
{
    public string title;
    public bool owned;
}

Code: Select all

public class WorldsData : ScriptableObject
{
    public WorldData[] worlds;
}

Code: Select all

public class WorldData : ScriptableObject
{
    public LevelData[] levels;
}

Code: Select all

public class LevelData : ScriptableObject
{
    public string title = "newLevel";
    public int money;

    // These have to be saved
    public bool playerVictory;
    public int playerScore;
    public int playerScraps;
}
Here is the generated json save :

Code: Select all

{
    "player":
    {
        "__type": "PlayerData,Assembly-CSharp",
        "value":
        {
            "_ES3Ref": 5364206003884784654,
            "nickname": "Toto",
            "score": 500,
            "items": [
            {
                "_ES3Ref": 8329346111485538119,
                "owned": false
            },
            {
                "_ES3Ref": 5534608772170016919,
                "owned": false
            }],
            "worlds":
            {
                "_ES3Ref": 1311634918222638909
            }
        }
    }
}
Here is what I expect to have :

Code: Select all

{
    "player":
    {
        "__type": "PlayerData,Assembly-CSharp",
        "value":
        {
            "_ES3Ref": 5364206003884784654,
            "nickname": "Toto",
            "score": 500,
            "items": [
            {
                "_ES3Ref": 8329346111485538119,
                "owned": false
            },
            {
                "_ES3Ref": 5534608772170016919,
                "owned": false
            }],
            "worlds":
            {
                "_ES3Ref": 1311634918222638909
                "worlds" :
                [
                    {
                        "_ES3Ref": 000000000-some-id
                        "levels" :
                        [
                            {
                                "_ES3Ref": 000000000-some-id,
                                "playerVictory" : false,
                                "playerScore" : 0,
                                "playerScraps": 0
                            },

                            {
                                "_ES3Ref": 000000000-some-id,
                                "playerVictory" : false,
                                "playerScore" : 0,
                                "playerScraps": 0
                            }
                        ]
                    },

                    {
                        "_ES3Ref": 000000000-some-id
                        "levels" :
                        [
                            {
                                "_ES3Ref": 000000000-some-id,
                                "playerVictory" : false,
                                "playerScore" : 0,
                                "playerScraps": 0
                            },

                            {
                                "_ES3Ref": 000000000-some-id,
                                "playerVictory" : false,
                                "playerScore" : 0,
                                "playerScraps": 0
                            }
                        ]
                    }
                ]
            }
        }
    }
}
# ISSUE 2

When I load my saved game, the inspector shows me wrong info or empty fields :(

Here is my inspector before loading of the save :
Capture-before.PNG
Capture-before.PNG (17.68 KiB) Viewed 3454 times
Here is my inspector after loading :
Capture-after.PNG
Capture-after.PNG (17.01 KiB) Viewed 3454 times
Can you help me with this please ?

Thanks !
Last edited by Rafarel on Tue Mar 12, 2019 8:34 am, edited 5 times in total.
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Save children of ScriptableObjects

Post by Rafarel »

I've been investigating my issue with with ES3 and I have some ideas.

If you ask ES3 to save a ScriptableObject that has a reference to another Scriptable object that will doesn't work well, ES3 writes only the ref but not the fields i'm interested in.

If you ask ES3 to save a ScriptableObject that contains an array of ScriptablesObjects, each ScriptableObject of the array will be perfectly written to the save file with all fields selected in the ES3 Type.

In the following example I have a ScriptableObject of type PlayerData that is saved under "player" id with ES3.
This Object contains a string, an integer, an array of ItemsData ScriptableObjects (items) and a single ItemData (item).
The items in the array are well serialized and saved, but the single item save only his reference and not its fields. Why ?

Here is the PlayerData :

Code: Select all

public class PlayerData : ScriptableObject
 {
     // This should not be saved
     public string noSave;
     
     // This has to be saved
     public string nickname = "player";
     public int score;
     public ItemData[] items; // These will be saved with their field.
     public ItemData item; // This will only save the reference to the object but not the fields.
 }
Here is the ItemData :

Code: Select all

public class ItemData : ScriptableObject
{
    // Do not want to save this so exclude it with the ES3 Types
    public string title;
    
    // This one is included in the ES3 Type
    public bool owned;
}
And here is the final saved JSON :

Code: Select all

{
    "player":
    {
        "__type": "PlayerData,Assembly-CSharp",
        "value":
        {
            "_ES3Ref": 5364206003884784654,
            "nickname": "Toto",
            "score": 500,
            "items": [
            {
                "_ES3Ref": 8483108343973745098,
                "owned": false
            },
            {
                "_ES3Ref": 8380875233458903929,
                "owned": false
            }],
            "item":
            {
                "_ES3Ref": 8483108343973745098
            }
        }
    }
}
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Save children of ScriptableObjects

Post by Rafarel »

And about the weird loading behaviour, it appears that by generating manually references with the "Refresh References" button on the Manager it worked.
I don't know why these are not automatically generated ?

Thanks for reading me it's a pretty long post with 2 different problems, I hope you can give me a fix or an explanation on how this works :)
Have a nice day !
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save nested ScriptableObjects

Post by Joel »

Hi there,

Regarding the issue of the missing references, it sounds like Auto Update References is disabled in Easy Save's settings, or there is a bug at Unity's end which caused the Play event to not be called.

With regards to the ScriptableObject, fields are saved by reference. However, items in arrays are saved by value and by reference, hence the behaviour you're experiencing. If you PM me a basic project, I'll show you how you can create an ES3Type which forces it to save by reference and value, rather than just reference.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Save nested ScriptableObjects

Post by Rafarel »

Hello,

Thanks for your answer.
Auto update references is well checked... will tell you if the issue persist.

For the project in PM what do you need ? A compressed archive of the Assets folder ?
Thanks for the help !
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save nested ScriptableObjects

Post by Joel »

A compressed archive of the entire project folder is recommended, incase the issue is due to meta data.

If necessary, you can update this to a service such as WeTransfer and send me the link.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Save nested ScriptableObjects

Post by Rafarel »

I've PM you the project, thanks for the support !
Post Reply