Loading Runtime-Generated ScriptableObjects

Discussion and help for Easy Save 3
Post Reply
ShaunBartoo
Posts: 5
Joined: Wed May 05, 2021 11:09 pm

Loading Runtime-Generated ScriptableObjects

Post by ShaunBartoo »

Hi,

I'm just getting going with this asset. So far its been great but I've hit a snag. I'm having trouble serializing a fairly complex component.

I'm working on a card game, and I have a Deck component I'm trying to save/load. The deck has a list of Cards (a non-MonoBehaviour c# class), and each card in the list has a scriptable object called CardData, which has nested CardEffect scriptableobjects. The CardData is a runtime-instantiated copy of the base CardData asset for the card, because each card's effects can change over the course of the game, so they can't all share the same scriptable object.

When I LoadInto the Deck component, the list of cards saves/loads fine, and the serialized reference to the CardData is preserved, but neither the CardData scriptable object nor its nested child scriptable objects are saved by value, so if a scriptable object's value is changed or the object is deleted, loading the deck component doesn't restore the CardData's original values or create it if it doesn't exist.

I've gotten ScriptableObjects to save/load fine in other cases in this project. I'm not sure what I'm doing wrong here. I've created a UserType for the Card class, and I'm using a ES3Settings with ReferenceMode.ByRefAndValue.

Some guidance on how to approach this complex serialization challenge would be greatly appreciated. Is there a way to load the value of ScriptableObjects referenced in fields of a loaded component?
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading Runtime-Generated ScriptableObjects

Post by Joel »

Hi there,

Are you saving your CardData ScriptableObjects being saved separately? Fields of UnityEngine.Object types are stored by reference, and a reference to runtime-generated objects will no longer exist when you leave the scene, so they need to be stored separately to ensure they're saved by value.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
ShaunBartoo
Posts: 5
Joined: Wed May 05, 2021 11:09 pm

Re: Loading Runtime-Generated ScriptableObjects

Post by ShaunBartoo »

Hi Joel,

Thanks for clarifying that. I misunderstood how ES3 serialized Scriptable Objects I think. I have a few followup questions. If I save the nested Scriptable Object's parent object directly, will the child objects should serialize as well? Does directly loading a Scriptable Object create an instance of it if it doesn't exist in the scene, or do I need to manually ensure there's an instance before I load data into it?

Thanks for your support
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Loading Runtime-Generated ScriptableObjects

Post by Joel »

Hi there,

I'm not sure what you mean by child objects. ScriptableObjects do not have a parent/child relationship in the same way that GameObjects do.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
jrbaxendale
Posts: 3
Joined: Sat May 22, 2021 6:16 pm

Re: Loading Runtime-Generated ScriptableObjects

Post by jrbaxendale »

Joel wrote: Thu May 06, 2021 9:22 am Hi there,

Are you saving your CardData ScriptableObjects being saved separately? Fields of UnityEngine.Object types are stored by reference, and a reference to runtime-generated objects will no longer exist when you leave the scene, so they need to be stored separately to ensure they're saved by value.

All the best,
Joel
Hi Joel; the above looks like the answer to my problem (where the saving works fine during the game for my runtime generated objs but doesnt work after I close and then re open the program). However, I am not sure exactly what you mean in the above where you say I need to store refs separately; can you expand?

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

Re: Loading Runtime-Generated ScriptableObjects

Post by Joel »

Hi there,

You would need to call ES3.Save on the object separately so that it stored by reference and value, rather than only saving it as a field (which will mean it's only stored by reference).

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