Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persisting save data through multiple scenes #1

Open
a2937 opened this issue Mar 9, 2022 · 6 comments
Open

Persisting save data through multiple scenes #1

a2937 opened this issue Mar 9, 2022 · 6 comments

Comments

@a2937
Copy link

a2937 commented Mar 9, 2022

Hi there. I just noticed something that kinda seems important. In your system , in order to persist save data from one scene to the next, the game data has to be saved and then loaded in the next scene otherwise all the data gets lost. I'm not quite sure this is the ideal solution. In this particular case would it be acceptable to have the DataPersistenceManager game object persisted into the next scene and have the other copies be destroyed if they're not as current? Or would it just be easier to automatically save and load using the SceneMananger.sceneLoaded and SceneManager.sceneUnloaded events? I would love to hear your thoughts.

@shapedbyrainstudios
Copy link
Owner

Hey! You are correct that in the current setup, the most straight forward way to deal with scene transitions would be to save the data before you transition scenes and then load it upon entering the new scene. I agree that SceneManager.sceneUnloaded and SceneManager.sceneLoaded are two good spots to help manage this. For my game, this is generally how I've been going about it.

For the DataPersistenceManager itself, you could either have the DataPersistenceManager in each scene, or like you mentioned, have it persist through the scenes using DontDestroyOnLoad. It'll all depend on how you handle the above. Here's some docs for DontDestroyOnLoad, which I think would work well for the DataPersistenceManager - https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

There are a lot of ways to go about scene transitions though. I'm thinking that the next video tutorial I do might be loading data from a main menu and transitioning scenes. I've gotten a lot of questions from previous tutorials on how to persist things across scenes. I'd want to research it more to make sure I'm presenting a good solution that'll work for the majority of people - so it might be a little while before that video makes it out.

With all of that said, if you have a game that has lots of scene transitions and you want them to be as seamless as possible, then the save/load before and after scene transitions may not be ideal and you'd likely have to make some modifications to this system or come up with a more clever way to persist the data across scenes.

I hope that helps a bit!

@a2937
Copy link
Author

a2937 commented Mar 9, 2022

I like the Don'tDestroyOnLoad idea but the problem is though I'm not 100% sure everything will work correctly. And I'm a little concerned about how the IDataPersistence will properly play into this. I'm going to proceed to fork this project and see what I come up.

Also as a heads up, I'll wait until your next video is released before I add a button to the Coin Inspector for generating the GUID rather than clicking a button. I also might be tempted to make your FileDataHandler use an interface instead to allow tutorial followers to have an easier time developing their own save formats and such. We'll see.

@richardelms
Copy link

Maybe you can get some inspiration from my library https://github.com/richardelms/FileBasedPlayerPrefs.
A Game data system should have nothing to do with the state of loaded scenes IMO

@a2937
Copy link
Author

a2937 commented Mar 11, 2022

I've gotten something created that is probably similar to what you have for your game on my local fork with the understand that in between scenes the file system isn't touched at all.

@a2937
Copy link
Author

a2937 commented Mar 11, 2022

My fork

@a2937
Copy link
Author

a2937 commented Mar 11, 2022

I was initially concerned that the player position would automatically be overwritten with the spot of any potential scene transfers and turns out it was a completely valid concern. I've managed to get past it by setting the player location before the player gets transferred so the correct data is present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants