Problems saving references to ScriptableObjects

Discussion and help for Easy Save 3
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Problems saving references to ScriptableObjects

Post by Rdbk »

I'm saving references to ScriptableObjects (assets created in Editor, ID doesn't change between runs), but when loaded, these references have disappeared.

Longer story:
-I have a SO "CharacterTemplate" that is used to create instances of this SO in run-time. These SO instances are added to a list called "Team" and then this list is saved with ES3: "ES3.Save<List<CharacterTemplate>>("Team", ProgressManager.instance.Team);". I then load it with: "ProgressManager.instance.Team = ES3.Load<List<CharacterTemplate>>("Team");". This works fine.
-I have a SO "Skill" that is used to hold varying data for different skills. These are not instanced and are only configured in editor. The abovementioned CharacterTemplates have references to these "Skill" SO's to tell which skills a specific character can use (these can change within the game). The problem is with these references disappearing.
-I had this problem before and it went away (= the references were in place when loading) for a while by just reinstalling ES3 (it was actually the same version IIRC). This time reinstalling did not work though.
-The weird thing is that there is actually one skill that ES3 remembers the reference for properly and this was the case before too when I had the problem (I think that skill is the first one I created if it makes any significance).
-Unfortunately I am not able to send an example project as this is a full-scale project. I can however give more exact details if needed.
-I am only using those two lines of code for saving & loading the character details. I assumed it was OK since it was working properly at one point, but I can try other ways too if that doesn't seem correct.
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

Is there a reference to your ScriptableObjects in your scene in the Editor?

If not, you will need to add these otherwise the reference manager will not have any knowledge of them. I also recommend pressing the Refresh References button of the ES3ReferenceMgr attached to the Easy Save 3 Manager, to ensure that all references have been gathered.

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 »

Joel wrote:Hi there,

Is there a reference to your ScriptableObjects in your scene in the Editor?

If not, you will need to add these otherwise the reference manager will not have any knowledge of them. I also recommend pressing the Refresh References button of the ES3ReferenceMgr attached to the Easy Save 3 Manager, to ensure that all references have been gathered.

All the best,
Joel
I am not very proficient with Unity yet so please advise if there is a specific way they should be referenced in scene. Currently I have a script ("DataTables" singleton) that is attached to a game object with DontDestroyOnLoad - so it is present in every scene. This script has a list "ListOfSkills" that has all the skill SO's in it. When the game is started (before loading game), this list is iterated and 3 additional lists are created based on the SO data: "ListOfSkillsLevel1", "ListOfSkillsLevel2" & "ListOfSkillsLevel3". These are used to give out skills to characters based on rarity.

I tried pressing the refresh references button, but it had no effect. (Sidenote: opening the references list caused a massive amount of editor lag, forcing me to restart the editor).
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

To reference it in the scene there needs to be a script in your scene in the Editor (so prior to runtime) which contains a reference to the objects. The easiest way to do this is to simply create a UnityObject[] field to a script in your scene and add them to this. After doing so, press Refresh References in the ES3ReferenceMgr and this should ensure that it can be referenced.

Note that the reference list is only included for debugging at our end, so I don't recommend that you open it in large projects as Unity struggles to display large lists in the Editor.

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 am not sure if I understood correctly, but I think I already have that? As mentioned above, I have a gameobject that exists in every scene and this gameobject has a script with a list of all the skill SO's in the game. This list is filled in editor before runtime and it is not altered in any way during runtime. As the gameobject exists in every scene and the list stays populated the whole time, the skills should be able to be referenced in any scene? I assume the UnityObject[] field meant something like this, but please correct me if I misunderstood.
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Hi there,

Is the GameObject with this List in every scene in the Editor? Or is this object only in the scenes because it is marked as DontDestroyOnLoad?

If it is indeed in every scene, then I'm unsure why they're not being referenced. It's difficult for me to comment further without being able to see a project which replicates it. Would it be possible to create a basic project with a similar structure which exhibits the issue?

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 solve it now, although I have no idea why it works.

I had the DataTables GameObject in every scene in the editor too, but I noticed that the list of skills in those other scenes only had 1 skill (the only one that was working properly before). I then added all the skills in those lists too and... it didn't work. I tried to remove the GameObject, didn't work. I tried to add a separate GameObject in editor with list of all skills (& update references) to the scene that is loaded, didn't work. I tried to delay the loading of the team, didn't work. I actually tested iterating through the list of skills in code before loading and all the skills had proper references in the scene, but the loading just didn't work. Then after some struggling, I tried to clear the list of skills in the scene to be loaded so it has 0 references (references come from same object with DontDestroyOnLoad from previous scene) and... it worked. To double the confusion, I had another SO ("Perk") functioning the same way with the same problems and that started working also with this change even though I didn't clear the list of perks like I did for skills. Oh well, if it works, it works.
User avatar
Joel
Moodkie Staff
Posts: 4824
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Not entirely sure why that worked, but I'm glad it's working for you now :)

We've noticed that Unity's CollectDependencies method can be extremely unreliable, so we're currently working on an update which means that references will now be added to the manager manually by right-clicking them and selecting a menu item, rather than automatically adding them (because this seems to be where it runs into problems). This will also be accompanied by tools which make it easier to see precisely what is referenced in your scene, so it will be easier to debug in the future.

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 »

Great to hear! Looking forward to it!
Rdbk
Posts: 35
Joined: Sun Jun 09, 2019 3:58 pm

Re: Problems saving references to ScriptableObjects

Post by Rdbk »

It seems it was too good to be true as the problem resurfaced without making changes to my earlier fix. I tried redoing the earlier fix but it didn't work. The curious thing is that some skills persist after loading, but most don't. I noticed that most of the persisting skills are those that are referenced by some CharacterTemplate (which itself is referenced by the DataTables object present in every scene). Annoyingly I also found some that didn't fit into this pattern. But inspired by this, I tried to create a separate object specific to the scene-to-be-loaded with a reference to all the SO's. Didn't help. Then, without doing anything else, I moved the order of that object in the scene structure (still in root, but just to the very bottom when it was on top before). Now the problem is suddenly gone and loading works as it should. I tried deactivating the object and the loading still worked. This leads me to believe the problem wasn't fixed due to anything I intentionally did.

I am at a loss with this one. It is nice that the problem went away again, but I am worried that it will resurface again after I have released my game (this was ~4th time the problem appeared and disappeared). Next time it appears, I will have to test it separately with a deployed version of the game. If there are any hints or things I should try, any help is appreciated!
Post Reply