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,

If you're planning on returning to the scene where you make objects DontDestroyOnLoad, you might want to consider copy-and-pasting the Easy Save 3 Manager to the other scene instead. You could also try to destroy the duplicate manager when you enter the scene, but I'm not sure how you would guarantee that it is deleted before the manager performs its own checks in Awake().

Just to clarify, Auto Add Manager to Scene simply adds the Manager to the scene. This doesn't guarantee that the dependencies of other scenes have been added to that manager, or that the dependencies have the same instance IDs. So there was no way of the second scene of knowing that the ScriptableObjects existed. You might instead consider whether there is a way of creating the ScriptableObjects in the scene in which they're being used, rather than in the Main Menu. However, this very much depends on your project whether this possible, and the case may be that DontDestroyOnLoad is the only way in your circumstance.

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 tried it with copy-and-pasting, but that didn't work (problem resurfaced) so it seems like DontDestroyOnLoad is the way. The singleton approach indeed didn't work as I got the merging error. I have a script that is created when the game starts and exists until the game is closed. What if ES3Manager gameObject is inactive in editor and I use Start function in that script to turn it on - would this be a suitable solution or do you foresee problems with this? Based on quick testing, it seems to work.

Could you clarify how I could make sure the ScriptableObject is created in the scene to be loaded and not in menu scene? I do not instance these skill SO's at any point and they are just used to store info (which is retrieved through a reference). I have trouble understanding what this "creation" of SO actually means. Does it mean the creation of the object that references these SOs? That object persists through scenes so I assume not.
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Making the GameObject certainly seems like a good way of going about it, and I don't foresee any issues that would be caused as a result of doing that, so that would be your best bet.

Just to clarify my previous comment, I mean instead of declaring your ListOfSkills in the MainMenu scene and then using DontDestroyOnLoad, you declare it in the scene in which you're saving/loading. However, whether this is possible for your project entirely depends on the structure of your project and the purpose of declaring them in the Main Menu, so this might not be applicable to you.

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 »

Ah now I understand, thanks for the clarification! I will think if I could delay the declaration of the list until the next scene.

It seems that changing to one single ES3Manager with DontDestroyOnLoad instead of one per scene wrecked my map saving/loading functionality (I use ES3 to save <Vector3Int, TileBase> Dictionaries, one per TileMap layer and then with loading SetTile on a Tilemap layer by layer - saving and loading is done in a 3rd scene "Battle"). I assume this is also due to mismatch with the references. I will try to play around with this tomorrow if I can come up with a solution. Any ideas for a simple fix? Sorry, it seems I keep creating new problems :)

EDIT: I decided to try saving the dictionary without EasySave3 to reduce dependencies and after a lot of struggling, the results seem promising (managed to serialize one dictionary, save that to json and then print one layer with that json file).
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,

Having a single Easy Save 3 Manager will mean that only the references of one scene will be included in the manager.

Let me know how you get on with the new Dictionary method you speak of :)

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 got the new dictionary method working... except that after restarting Unity, all the loaded maps were scrambled. Oh well. I decided to give the ES3 option another go and refactored my data scripts so that they are created in the scene where they are used. So now I have one ES3Manager per scene (without DontDestroyOnLoad) and so far the loading seems to work properly with the SO's. I also re-refactored my map saving/loading script and it seems to save and load the maps properly (even after restarting Unity).

So HOPEFULLY (please be so :roll: ) everything is working as it should now and I can let this part of the code be as-is. Thanks again for the help! Understanding the significance of when the lists are declared was vital in solving this!
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Problems saving references to ScriptableObjects

Post by Joel »

Glad to hear that is (hopefully!) working for you now, let me know if you do come across any other problems :)

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 ran into a problem which seems to be solved now and might not be related to this, but I figured I check with you if it might be related.

-The problem was with my inventory (List of SO's) loading as null, even though it has always worked properly before. After a lot of debugging, I couldn't fully grasp the logic and there were some weirdness (for example the problem not showing in a built version vs unity editor version with no other changes), but I came to the conclusion that the problem was likely due to my script execution order changing for whatever reason (there were no changes related to this so that's still a mystery), which caused the save file being rewritten with a new save before the values were properly loaded from the old save (I have my different components loading their values from ES3 in their respective Start() functions so there's some potential for timing issues). But related to this I would like to check 2 things with you: 1) Do you know of any ES3 issues that manifest in Unity editor but not in built versions (grasping at straws maybe, but that was a noticeable factor in this) & 2) Do you see any potential reference issues with creating a list of SO's with DontDestroyOnLoad and adding instantiated SO's to it in scene A, then loading scene B (LoadSceneMode.Single) and finally loading scene A again and saving the list with ES3. The game is then closed in between and then upon a new startup, scene A is loaded with the list. This has been working great aside from this recent problem, but I was wondering if you see some potential issues with the references in a situation like this (and if then this problem could be somehow connected to that).
-Also, since the original problem is solved, I want to disable the generating of the DebugFile.txt file. With search I found 4 lines that mentioned "DebugFile.txt" and commented those out. Is this good enough of a solution or do you foresee any issues? I got a warning about inconsistent line endings after that (I use windows) and converted them to Windows-specific, but I assume that won't have an effect on the functionality.
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,

The only issue we really encounter when going from Editor to a built player is that people don't always account for the case where there is no initial save data, because they've been testing in the Editor where they've never deleted the save data they've created.

With regards to your second question, I don't foresee any issues apart from the ones which have already been mentioned when working with cross-scene references (i.e. a reference manager in one scene not having references to an object in another scene).

On your third point, you're correct, commenting out the DebugFile.txt will stop the debug file being created. The inconsistent line endings warning is harmless. Visual Studio has a Line Endings Unifier plugin if you wanted to stop this warning: https://marketplace.visualstudio.com/it ... ngsUnifier

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