Page 1 of 3

Persist Scriptable Objects between scenes

Posted: Tue Sep 22, 2020 12:56 pm
by JoseHawk
Hello,

During runtime, it is possible to collect items, and these are saved in the inventory as a new Instantiate of the object. After saving and loading the "inventory" information, these scriptable objects are null.

I tried an alternative creating a serialializable object to save in my inventory instead of the scriptable object, but also I have the same problem when assigning a sprite or a material, or any other non-primitive variable.

Could someone help me with this?

Thanks!

Re: Persist Scriptable Objects between scenes

Posted: Tue Sep 22, 2020 6:06 pm
by Joel
Hi there,

This should indeed be possible using ES3.Save and ES3.Load. If you are encountering issues, please could you create a new project with a basic scene which replicates your issue and private message it to me?

All the best,
Joel

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 6:20 am
by JoseHawk
Hello,

This is not working for me with these methods. For example, if I would like to save a List of ScriptableObject that I got on runtime, when I run the game, I have a list of the same size, but this is empty.

BTW, is it possible to save the Animator last parameters?

Cheers.

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 9:03 am
by Joel
Hi there,

I've tried to replicate this with the following scripts but it appears to be working fine at my end:

Code: Select all

using UnityEngine;

public class MyScriptableObject : ScriptableObject
{
    public string myString = "defaultString";
}

Code: Select all

using System.Collections.Generic;
using UnityEngine;

public class SaveScriptableObjects : MonoBehaviour
{
    List<MyScriptableObject> scriptableObjects = new List<MyScriptableObject>();

    void Start()
    {
        scriptableObjects = ES3.Load("scriptableObjects", scriptableObjects);

        foreach (var scriptableObject in scriptableObjects)
            Debug.Log(scriptableObject.myString);
    }

    void OnApplicationQuit()
    {
        var scriptableObjects = new List<MyScriptableObject>();

        for(int i=0; i<3; i++)
        {
            var instance = ScriptableObject.CreateInstance<MyScriptableObject>();
            instance.myString = i.ToString();
            scriptableObjects.Add(instance);
        }

        ES3.Save("scriptableObjects", scriptableObjects);
    }
}
With regards to Animators, they're not automatically serialisable and explicit support hasn't been requested before. I've added a feature request for this here:
https://moodkie.com/forum/viewtopic.php?f=14&t=2027

By the looks of it, you would need to call the relevant Get/Set methods on the animator to get the different aspects of it, and serialise these.

All the best,
Joel

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 9:49 am
by JoseHawk
Thanks Joel!

I will give it a try and will let you know if I have some question.

Have a nice day.

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 10:21 am
by Joel
Here's some instructions for using the above scripts just in case you need them :)
  1. Add the SaveScriptableObjects script to any object in your scene.
  2. Enter Playmode
  3. Exit Playmode. This will generate the ScriptableObjects and save them.
  4. Enter Playmode again. It will find the save data and load the ScriptableObjects. You will get some warnings to console but these are expected when dynamically loading objects.
All the best,
Joel

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 3:17 pm
by JoseHawk
Hello,

It worked more or less fine. For some reason, some gameObjects that are using some Sprite from these Scriptable Objects are displaying the default Sprites, even if I try to hardtype it... But I will look more deep on this trouble.

I wanted to ask you about another issue. I have an object (room) in my scene that has some children. I added this object to the auto-save and selected only one interested field in order to save, but when I load the game, the Animator/SpriteRenderer from the children of this object looks broken. I attach you some images about this issue.

How can I also delete from the "prefabs" tabs? I tried deleting the generated folder, but it didn't work.

Thank you in advance.

Cheers!

Re: Persist Scriptable Objects between scenes

Posted: Wed Sep 23, 2020 4:30 pm
by Joel
Hi there,

Unfortunately it wouldn't be possible to tell from what you've posted.

As before, please could you create a new project with a basic scene which replicates this and private message it to me so I can look into it further?

From what I can tell, if you're only saving that script then it will not have an effect on the animator unless there's something inside your script which affects it. You might want to try going to Window > Easy Save 3 > Tools > Clear Persistent Data Path to ensure that there's not any old save data which is being loaded.

All the best,
Joel

Re: Persist Scriptable Objects between scenes

Posted: Thu Sep 24, 2020 12:31 pm
by JoseHawk
Hello,

Sorry for disturb you again. Step by step I am solving issues, but new ones are coming.

I explain the context:
  • I am selecting the objects that I desire to save from the Easy Save menu, selecting the fields I want to save from the desired script (see attach 1).
  • IWhen saving the game, I save the player information, scene Index and all the "current" selected stuff.

Code: Select all

public void SaveGame(Player player, int sceneIndex)
        {
            ES3AutoSaveMgr.Current.Save();
            ES3.Save("scene_index", sceneIndex);
            SavePlayerInformation(player);
        }
 
  • If I load the scene with ES3AutoSaveMgr.Current.Load(), everything loads great in the state I left.
  • BUT if I move to another scene and come back here, even if the ES3AutoSaveMgr.Current.Load() is happening, nothing is actually loaded. The new scene in which I move also has an "Easy Save 3 Manager"
I am not understanding something about the save/load process?

Re: Persist Scriptable Objects between scenes

Posted: Thu Sep 24, 2020 6:14 pm
by Joel
Hi there,

It sounds like perhaps you are trying to load references to objects which are created at runtime, which means an object with that reference ID will no longer exist when you exit the scene and come back.

If this isn't enough to solve the issue, please could you create a new, very basic project which replicates this so I can take a look?

All the best,
Joel