Page 1 of 2

Scriptable Object variable value is being removed when loading

Posted: Tue Jun 01, 2021 7:03 am
by robotSmith
Hello,

I'm not sure if you have seen this before, but when I'm doing a load a variable is being removed. I have a Scriptable Object (item) that is being stored in a Dictionary. Now, one group of items within this Dictionary have one variable that holds another Scriptable Object (like an enum) as a variable. When I load the dictionary, this Scriptable Object (that works as an enum) is being removed from the item Scriptable Object.

This has happened to me before in a smaller project, but I did not pay much attention to it as it was a prototype.

I'm attaching a video with the problem. You can see in the inspector the variable Container Type. It becomes "null" (none) when I load the game.

TIA!

Re: Scriptable Object variable value is being removed when loading

Posted: Tue Jun 01, 2021 7:57 am
by Joel
Hi there,

Are your getting any warnings to console when loading? If not, please could you create a self-contained script(s) which I can drop into a new project which replicates this?

Also just to check, is the ScriptableObject you're storing in a field saved elsewhere? If it's only being saved as a field, it will only be saved by reference.

All the best,
Joel

Re: Scriptable Object variable value is being removed when loading

Posted: Thu Jun 03, 2021 2:27 am
by robotSmith
Hello!

- I'm not getting warning when loading
- The Scriptable Object exist in the file system as it's created using the Create/Menu option.

I have been trying to reduce the project to find the reason the bug is happening but it has been challenging, it certainly appears every time I load. If I close and reopen Unity the variables will appear back.

Now, I tried duplicating the project to start doing a bit more drastic testing to find the bug, but I found a different issue (probably because of the duplication?). I'm getting the error message

Code: Select all

FormatException: Expected ',' separating properties or '"' before property name, found 'n'.
I just tested saving something as simple as an Integer, and I get that message. I have cleared out the Persistent Path as you have advised in other threads and it does not fix it. When I open the save file using notepad++ it reads:

Code: Select all

null : { null : null, null : null }
This is the stack trace:

Code: Select all

FormatException: Expected ',' separating properties or '"' before property name, found 'n'.
ES3Internal.ES3JSONReader.ReadPropertyName () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3JSONReader.cs:63)
ES3Reader+ES3ReaderRawEnumerator+<GetEnumerator>d__2.MoveNext () (at Assets/Plugins/Easy Save 3/Scripts/Readers/ES3Reader.cs:436)
ES3Writer.Merge (ES3Reader reader) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:466)
ES3Writer.Merge () (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:456)
ES3Writer.Save (System.Boolean overwriteKeys) (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:482)
ES3Writer.Save () (at Assets/Plugins/Easy Save 3/Scripts/Writers/ES3Writer.cs:474)
ES3.Save[T] (System.String key, T value, ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:103)
ES3.Save[T] (System.String key, T value) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:63)

Re: Scriptable Object variable value is being removed when loading

Posted: Thu Jun 03, 2021 6:40 am
by robotSmith
Ah, it seems that it was a problem with the latest update that you already fixed. So you can omit the last message about the FormatException. I'll try to reproduce the other error in a smaller scale

Re: Scriptable Object variable value is being removed when loading

Posted: Fri Jun 04, 2021 3:31 am
by robotSmith
Okay, so I did get some Warnings that were hidden in between other warnings that I had. The warning I'm getting is this one:

Code: Select all

Reference for ContainerType with ID 2082928117821905327 could not be found in Easy Save's reference manager
Now, this seems to be a very picky bug. It happens with certain items yes and not with others. The Scriptable Object is being created on the file system. The file is in a variable of another Scriptable Object, and that Scriptable Object is within a Dictionary. I save and load the dictionary, and when I do that, that variable sometimes gets emptied out.

I was able to reduce my project to bare bones and still have the problem.

Re: Scriptable Object variable value is being removed when loading

Posted: Fri Jun 04, 2021 7:49 am
by Joel
Hi there,

This warning occurs because you're trying to load a reference to an object which doesn't exist. For example, it may be that it has not been saved, it has been saved but not loaded yet, or this object was cloned by Unity before it was saved meaning the reference will no longer exist after you leave the scene.

In this circumstance you would need to save it separately, and then load it before attempting to load anything which references it.

All the best,
Joel

Re: Scriptable Object variable value is being removed when loading

Posted: Fri Jun 04, 2021 4:00 pm
by robotSmith
Hello Joel,

Thanks for the answer!

My assumption was that ES3 would not touch Scriptable Object files that were used (and saved) by reference and only load the reference without touching the file. But it seems that ES3 is reloading the object too even if it's a reference.

Now, why do you think happens intermittently? For example, that Item Scriptable Object has another variable (foo) that is now empty, if I fill it out in the Inspector, the problematic variable (bar) would not be emptied out. The other Items that have this other variable empty (foo) have the problem where the problematic variable (bar) are getting emptied out. Why would the change of this variable (foo) affect the emptying out of another variable (bar) on loading?

Re: Scriptable Object variable value is being removed when loading

Posted: Fri Jun 04, 2021 4:19 pm
by Joel
Hi there,
My assumption was that ES3 would not touch Scriptable Object files that were used (and saved) by reference and only load the reference without touching the file. But it seems that ES3 is reloading the object too even if it's a reference.
I'm afraid I'm not sure what you mean by this. Please could you explain?

Just to clarify, Easy Save won't be the cause of new references being created or the reference not existing, this will be external to Easy Save. Easy Save simply looks for the reference with the given ID, and if a reference with the given ID is not found it will throw that warning.

All the best,
Joel

Re: Scriptable Object variable value is being removed when loading

Posted: Fri Jun 04, 2021 11:31 pm
by robotSmith
I attached an image that can explain my assumptions.

- I save and load the Item Database only.
- Let's say we have Item, Item Type, and Category. All three are Scriptable Objects created on the inspector
- Item has a variable for Item Type and Category.
- Item is part of the dictionary

1. When I save and load the Item Database dictionary, I thought ES3 would not touch Item, Item Type, and Categories. I thought it would just load the dictionary info with the references (and not try to "load" Item and Item Type, but I prob don't understand the inner workings of the asset).
2. When I load the Item Database, sometimes the Item Type variable within some Item objects gets emptied out, and I get that warning (but it's not easy to tell why sometimes it does and why sometimes it doesn't).
3. From my testing (and shown in the image), if I add the Category SO to the Item, then on loading, the Item Type does not get emptied out. If Category variable is null in the Item, then Item Type gets emptied out.

Re: Scriptable Object variable value is being removed when loading

Posted: Sat Jun 05, 2021 7:57 am
by Joel
Hi there,

ScriptableObject fields should be serialized by reference. If you're saying that it's being serialized by value, I've not seen this happen before without the member reference mode being manually set to Value or RefAndValue in the settings.

Please could you delete your save file (Tools > Easy Save 3 > Clear Persistent Data Path) and generate a new one containing just this information, and post it along with the contents of the Assets/Easy Save 3/Types/ folder (if it exists) and the code you're using to save and load.

All the best,
Joel