Problems saving references to ScriptableObjects

Discussion and help for Easy Save 3
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

It sounds to me that Unity is losing the reference to the instance in the Editor, though it's not possible for me to tell for certain without access to a project which replicates this. Is there any possibility of PM-ing me a project which exhibits the error?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

Is there some way I could test this? Sending the actual project is not possible and trying to replicate with another project could be problematic as I cannot replicate the problem even with my current project (it seems to just come and go). I can try it soonish, but I am not very optimistic I will be able to replicate it.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

I've created an update which I'll PM to you which outputs what is happening when reading the ScriptableObjects (i.e. whether the ID exists, or whether it's creating an instance of it) to a log file in Application.persistentDataPath. If you take a look at this file if the issue occurs again, it might give us some insight into what is happening.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

Hey,

I added some new skills to the game and the problem reappeared. But this time it was obvious that only the newly added skills were missing after loading - the older skills were fine. I add skills by entering their details into .xls file, from which I copy all into a .txt file and then parse the file in Unity with tab separating the values and create a new skill ScriptableObject from each value (if a skill already exists with the name, it only updates the existing skill). I did some testing with the "find references in scene" which found some differences. Here's a compilation picture I will reference: https://imgur.com/a/8nu9hst

-I didn't notice any changes in the id's of the new skills and they didn't disappear when I restarted Unity so I assume they were created properly in Unity editor.
-The DebugFile.txt didn't have any clues. It shows just the same line as before: "11/24/2019 5:20:08 PMSObj with id -1 was NOT found in the reference manager, so an instance has been created with that ID"
-Update references in EasySave3Manager didn't have any effect. The "Town" scene is the scene that is always loaded. This scene doesn't have the skill references, but they are inherited from "MainMenu" scene as a DontDestroyOnLoad Object that fills the same object in "Town" with the references. This works just fine and the older skills are loaded properly. I tried adding the skill references to the "Town" scene in editor too and then update references in ES3Manager, but that didn't have any effect.
-In picture #1 is a new skill in run-mode that isn't loaded properly. #2 is an older skill that is loaded properly. As can be seen, GlobalVariables object that has all the references holds the reference properly, but ES3manager doesn't. The other missing objects are not that relevant from what I can gather - they are just related to different objects that have reference to the character that has the skill.
-#3,4,5 are in editor view. "Mana Bomb" skill is an older skill that is referenced properly in ES3 in "Town", but "Fleeting Pressure" as a new skill is not referenced at all in "Town". This skill is references properly in the "MainMenu" scene however.
-#6,#7 are pictures from before saving comparing the old and new skill. Based on this it doesn't seem there is an obvious reason why they would be different before saving.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

Does this happen every time you add a new skill? And if so, please could you PM me a basic project to replicate it? Although I very much appreciate the detailed information you've given me, it's still not enough for me to understand what is happening.

Also would you be able to give me more information on the lifecycle of your ScriptableObjects? I.e. are they created at runtime, is there an instance in your scene prior to runtime, or do they only exist in the Assets folder? Are they only used in one scene, or do you use DontDestroyOnLoad to use them in a different scene?

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

Hey Joel,

I will get to your questions in just a moment. Here's an update on the problem:

-I tried to manually create a skill in editor, no difference
-Deleting (& respawning) ES3Manager didn't help
-I have a ScriptableObject type ("DebugSO") that I created for debugging this, which I am referencing in all scenes. It has just a list of different ScriptableObjects that I can fill. The list for skills was empty on it so I added all the skills to it. This actually fixed the problem - for the time being. Funny thing is, I then tried to empty this list again and even remove any reference to the DebugSO in my scenes, but the problem still remained fixed. So it would seem that adding the skills to this list in the DebugSO somehow triggered ES3 to actually remember them.
-I now tried adding a new skill (manually) and the problem reappeared just for this skill (the previous newly added ones didn't have any problems). So this would indicate that the problem indeed appears every time new skills are added and only for those newly added skills.

I have different types of ScriptableObjects, but if I take these "Skill" SOs as an example, the lifecycle goes like this:

-I fill the skill details in a .csv file which is then used to fill a .txt file where tabs separate each value.
-I then use a Unity Editor script, which parses this .txt file and creates a new asset file based on each value (or updates existing asset with same id).
-These skills are then added to a list "ListOfSkills" which exists on a "DataTables" object. This object is present in every scene as a DontDestroyOnLoad object.
-In run mode, an instance of another ScriptableObject "CharacterTemplate" is created. This SO has a method called "Initialize" which among other things pulls a skill or two from the ListOfSkills in the DataTables object. These skills are stored as a reference in the CharacterTemplate. The skill SO's are not instanced at any point. So I believe this means the skills only exist as an asset in the Assets folder before runtime (& referenced in an object).
-My game has only 3 scenes: MainMenu, Town & Battle. The skills are referenced in each of them through the DataTables object and then in Town & Battle they are also referenced by the CharacterTemplate SO instances (& also by some "Equipment" SOs)

I have been busy lately, but I will try to find time soon for a test project to see if I could replicate this outside of my actual project.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

Thanks for the info. If you could try to create a basic project to replicate it that would be greatly appreciated because although the info you've provided is helpful, it's very difficult for me to be able to replicate it from what you've said.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

I managed to replicate it now with a new project. Sending you the project via PM.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Thanks for sending that over.

The issue is because you are creating your ScriptableObjects in your Menu scene, but you are saving and loading them in a different scene which will have no knowledge of these existing.

To resolve this you should either set the Easy Save 3 Manager to DontDestroyOnLoad, or copy-and-paste it into the scene you're saving and loading in, so that the reference managers from the two scenes can be merged.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

Putting DontDestroyOnLoad on the ES3Manager seems to indeed solve the issue in my actual project also. I had the "Auto Add Manager to Scene" option on with ES3Manager in every scene, but I assume that doesn't facilitate the merging then. Just to verify, I should probably now turn that option off and make it so that ES3Manager only exists in my first scene (Menu) and that will have the DontDestroyOnLoad? And if I go back to menu, I should delete the ES3Manager via code or maybe make the DontDestroyOnLoad script a singleton (with selfdestruction if it already exists)? Currently going back to menu will result in 2 ESManagers and an error about merging duplicate IDs.

Just out of curiosity regarding the issue (and apologies if this is a dumb question as I'm not too deep into the technicals), what does creating the ScriptableObjest in the menu scene mean in practice? I assumed nothing was creating them and they were just used as a reference with ES3 fetching the reference to the proper SO asset. Are they somehow created in the first scene when entering playmode and then forgotten in next scene? I think this is confusing me a bit because the way I load the game is that new scene is loaded first and then all the needed objects are created and references & values set with the help of ES3. I have a hard time understanding the logic what would have been the proper way to implement this without using the DontDestroyOnLoad.

Thanks for all the help! Glad the issue seems to have now been tackled.
Post Reply