Persist Scriptable Objects between scenes

Discussion and help for Easy Save 3
JoseHawk
Posts: 13
Joined: Tue Sep 22, 2020 12:51 pm

Persist Scriptable Objects between scenes

Post 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!

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Persist Scriptable Objects between scenes

Post 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
Joel @ Moodkie Interactive
Twitter - Unity

JoseHawk
Posts: 13
Joined: Tue Sep 22, 2020 12:51 pm

Re: Persist Scriptable Objects between scenes

Post 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.

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Persist Scriptable Objects between scenes

Post 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
Joel @ Moodkie Interactive
Twitter - Unity

JoseHawk
Posts: 13
Joined: Tue Sep 22, 2020 12:51 pm

Re: Persist Scriptable Objects between scenes

Post by JoseHawk »

Thanks Joel!

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

Have a nice day.

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Persist Scriptable Objects between scenes

Post 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
Joel @ Moodkie Interactive
Twitter - Unity

JoseHawk
Posts: 13
Joined: Tue Sep 22, 2020 12:51 pm

Re: Persist Scriptable Objects between scenes

Post 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!
Attachments
C.PNG
C.PNG (66.85 KiB) Viewed 256 times
B.PNG
B.PNG (49.38 KiB) Viewed 256 times
A.PNG
A.PNG (367.75 KiB) Viewed 256 times

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Persist Scriptable Objects between scenes

Post 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
Joel @ Moodkie Interactive
Twitter - Unity

JoseHawk
Posts: 13
Joined: Tue Sep 22, 2020 12:51 pm

Re: Persist Scriptable Objects between scenes

Post 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?
Attachments
A.PNG
A.PNG (132.59 KiB) Viewed 251 times

User avatar
Joel
Moodkie Staff
Posts: 2971
Joined: Wed Nov 07, 2012 10:32 pm

Re: Persist Scriptable Objects between scenes

Post 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
Joel @ Moodkie Interactive
Twitter - Unity

Post Reply