Scriptable Object is reset to null OnApplicationQuit

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

Scriptable Object is reset to null OnApplicationQuit

Post by Rafarel »

Hi there,

First of all I'm very happy with this plugin very good job :)
I have some trouble using EasySave with my scriptable objects, hope someone can help me here :)
I'm using the cache class example to achieve my player manager that is responsible of the player save.

I have a main ScriptableObject "PlayerData" that have an array of other SciptableObjects called "GameItems".
If I do save the PlayerData, all is saved correctly, but when quit the application the PlayerData Scriptable Object has its list of GameItems all reset to none.
The length of the array is not touched, just the content.
When I reload the save file, all goes back in place (except the names of the SOs).

Here is my data strucutre in two classes for now :

Code: Select all

public class PlayerData : ScriptableObject
{
    public string nickname;
    public int scraps;

    public GameItemData[] gameItems;

    public void Reset()
    {
        nickname = "Player";
        scraps = 0;

        foreach (GameItemData item in gameItems)
        {
            item.Reset();
        }
    }
}

public class GameItemData : ScriptableObject
{
    public string uid;
    public string title;
    public Texture2D image;
    public bool owned;

    public void Reset()
    {
        owned = false;
    }
}
And here is the class that handle the Player Saving and Loading :

Code: Select all

public class PlayerManager : MonoBehaviour
{    
    public  PlayerData player;

    public static ES3File file;
    public static PlayerManager instance = null;
    
    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
    }

    public void Start()
    {
        // Create a new ES3File. This automatically loads
        // the data from claytus.save into the ES3File.
        file = new ES3File("claytus.save");

        // If the save file exists load it
        // If not, reset the player data to default
        if (ES3.FileExists("claytus.save"))
        {
            Load();
        }
        else
        {
            Reset();
        }
    }

    public void OnApplicationQuit()
    {
        file.Sync();
    }

    public static void Save()
    {
        file.Save<PlayerData>("player", instance.player);
    }

    public static void Load()
    {
        if (file.KeyExists("player"))
        {
            file.LoadInto<PlayerData>("player", instance.player);
        }
    }

    private void Reset()
    {
        instance.player.Reset();
    }
}
[EDIT} On the following screenshots, don't pay attention to the nickname and scraps fields on the screenshots, these fields works perfectly :)

Here what looks like my inspector (I'm using Odin) before any save or load, these are the default values typed in the inspector :
File as its default values before any saving or loading
File as its default values before any saving or loading
Screenshot 2018-12-05 at 16.28.55.png (73.71 KiB) Viewed 4983 times
Here what does looks like my inspector after loading the player data (the game is still running) :
After loading
After loading
Screenshot 2018-12-05 at 16.33.06.png (70.4 KiB) Viewed 4983 times
And finally here is what looks like my inspector after quitting the application :
After application quit
After application quit
Screenshot 2018-12-05 at 16.36.26.png (73.25 KiB) Viewed 4983 times
Hope you can help me with his :)
Have a nice day!
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Scriptable Object is reset to null OnApplicationQuit

Post by Rafarel »

If this can't work, is anybody can suggest an alternative please?
Thank you very much
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable Object is reset to null OnApplicationQuit

Post by Joel »

Hi there,

Please could you PM me a very basic project to replicate this? If you could also let me know the version of Unity you're using that would also be helpful :)

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: Scriptable Object is reset to null OnApplicationQuit

Post by Rafarel »

Hello, I'm trying to reproduce it in a small project but I can't ... it works on a project with only these objects.
I don't know why it is a very simple PlayerData object.
Do you know if renaming the ES3 manager can cause issues ? I called it SaveManager
User avatar
Rafarel
Posts: 15
Joined: Wed Dec 05, 2018 3:16 pm

Re: Scriptable Object is reset to null OnApplicationQuit

Post by Rafarel »

Hi Joel,

I decide to remove everything from my project related to the object I wanted to save and redo all from scratch.
It works now :) I do not understand why but it is cool.
I think I had broken references at some point.
Have a nice day !
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable Object is reset to null OnApplicationQuit

Post by Joel »

Hi there,

Renaming the Easy Save manager can indeed cause issues as some Easy Save functions use the name to locate the manager. It could be that doing so meant that references were no longer being maintained, causing your error to occur.

Glad you managed to find a solution anyway!

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: Scriptable Object is reset to null OnApplicationQuit

Post by Rafarel »

Hi,

My issue described at the top of this post still persist but I found something interesting during my investigation.

Here is the setup :

I have two scenes : A my main menu and B the actual game.
I start fresh with an empty persistent data folder.
I'm using the technique described on the Caching using an ES3File guide.
Each scene has an Easy Save 3 Manager in its hierarchy.
I'm on Unity 2018.3.7f1

Here is the working case :

I start the game on scene A, do the save on scene A then quit.
The file is sync on Application quit.
Everything looks good in the generated json file.
Then I restart the game on scene A, the loading of the json happened on start of every scenes.
The PlayerData Scriptable object is well populated with the saved data, all is OK !

Here is the broken case :

I start the game on scene A my main menu, the I click a button to go scene B my game.
I do the save on scene B then quit.
The file is sync on Application quit.
Everything looks good in the generated json file.
Then I restart the game on scene A, the loading of the json happened on start of every scenes.
The PlayerData Scriptable object is badly populated with None ! Please refer to the screenshots of the initial post.

Please help me guys, maybe the issue has something with my other post I made today about having reference manager issues with values at None and different ids depending of scene that is right here : https://moodkie.com/forum/viewtopic.php?f=13&t=1584
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Scriptable Object is reset to null OnApplicationQuit

Post by Joel »

Hi there,

You will get null references because the instance IDs of the objects you've saved in Scene B will not exist in Scene A. In these cases you will need to save the data you wish to persist individually, and load it into the appropriate instance using ES3.LoadInto. Otherwise there's no way of determining which instance to load the data into.

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