我什么时候应该为 Google Play Saved Games 写快照?

When should I write a snapshot for Google Play Saved Games?

我正在将我的 libGDX android 游戏的保存系统更改为 Google 保存的游戏,我想自动保存和加载快照,所以用户没有例如,记得在卸载前保存。我的 "old" 系统使用本地 json 文件进行存储。我有一个 class 保存播放器的所有当前 "state" 值,并且每次我的用户更改屏幕时我都会将状态保存到文件中。它工作得很好,因为保存本身在我的屏幕被毁之前就已经完成了。现在,通过快照的保存过程需要保存机制在 AsyncTask 内。我更愿意在用户 closes 应用程序(或者它被 os closed 后写一个保存,但我不知道如何。如果我尝试写入快照,比如说 onPause(),写入失败,因为我用于它的 GameHelper 对象运行它的 onStop() 方法,而 asynctask 是 运行.有没有办法在应用程序退出时写入快照?

尝试此 github 论坛中建议的解决方案,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 使用之前打开的元数据 再次打开元数据,保留对打开元数据的引用。