I'm changing my save system of my libGDX android game to Google Saved Games, and I wanna save and load snapshots automatically, so the user doesn't have to remember to save before uninstalling, for example. My "old" system used local json files for storage. I have a class that holds all the current "state" values of the player, and I saved the state to file everytime my users change screens. It worked perfectly, as the saving itself got done before my screens were destroyed. Now, the save process through snapshots requires the saving mechanism to be inside an AsyncTask
. I would prefer to write a save once the user closes the app (or it gets closed by the os), but I don't know how. If I try to write the snapshot in, let's say onPause()
, the writing fails, as the GameHelper
object I use for it, runs through its onStop()
method while the asynctask is running. Is there a way to make writing snapshots on application exit work?
1 Answers
Try the suggested resolution in this github forum, How do we save game on pause or quit?:
Make sure to always keep the metadata available and open.
On first load
- Open metadata
- Load game
- Open metadata again, keep reference to open metadata
OnApplicationPause or OnApplicationQuit
- CommitUpdate using previously opened metadata.
OnApplicationPause (Resuming)
Open metadata again, keep a reference to open metadata. By keeping the metadata open at all times, you can always save OnApplicationPause. Remember we need to open the metadata again when the game resumes, because the CommitUpdate closed our metadata OnApplicationPause.
Using this method, you need to be prepared to handle file conflicts literally everywhere in your application - the user can pause the application at any time. When the application resumes you need to open the metadata - you cant open a metadata until any conflicts are resolved.
To add to this, if you want to save the game at an arbitrary point in time, (not on OnApplicationPause), then you will need to:
Saving at any other point (ie: Loading screen/level transition)
CommitUpdate using previously opened metadata Open metadata again, keep a reference to open metadata.