E. Game Save Snapshots - myflashlab/GameServices-ANE GitHub Wiki
Displaying Saved Games
You can integrate the Saved Games API wherever your game provides players with the option to save or restore their progress. Your game might display such an option at designated save/restore points or allow players to save or restore progress at any time.
Once players select the save/restore option in your game, your game should bring up a screen that prompts the players to enter information for a new saved game or select an existing saved game to restore. To simplify your development, the Saved Games API provides a default Saved Games selection user interface (UI) that you can use out-of-the-box. The Saved Games selection UI allows players to create a new saved game, view details about existing saved games, and load previous saved games.
To bring up the default Saved Games UI simply call showNativeWindow() passing in the title to display in the action bar of the native window. A Boolean to decide whether or not to display a "create new snapshot" option in the selection UI. Another Boolean to decide whether or not to provide a delete overflow menu option for each snapshot in the selection UI. And finally an integer indicating the maximum number of snapshots to display in the UI.
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_WINDOW_FAILURE, onSnapshotWindowFailure);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_WINDOW_DISMISSED, onSnapshotWindowDismissed);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_WINDOW_NEW, onSnapshotWindowNew);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_WINDOW_PICK, onSnapshotWindowPick);
Games.snapshots.showNativeWindow("my game saves!", true, true, 6)
Writing Saved Games
If the player selects to create a new saved game or load an existing saved game, the UI sends a request to Google Play games services. If the request is successful, Google Play games services returns through the following events GamesEvents.SNAPSHOT_WINDOW_NEW and GamesEvents.SNAPSHOT_WINDOW_PICK].
The following code snippet shows a sample implementation of the game saves UI:
function onSnapshotWindowFailure(e:GamesEvents):void
{
trace("onSnapshotWindowFailure: " + e.msg);
}
function onSnapshotWindowDismissed(e:GamesEvents):void
{
trace("onSnapshotWindowDismissed");
}
function onSnapshotWindowNew(e:GamesEvents):void
{
toSaveNewSnapshot();
}
function onSnapshotWindowPick(e:GamesEvents):void
{
trace("coverImageAspectRatio: " + e.snapshotMetadata.coverImageAspectRatio);
trace("description: " + e.snapshotMetadata.description);
trace("deviceName: " + e.snapshotMetadata.deviceName);
trace("hasChangePending: " + e.snapshotMetadata.hasChangePending);
trace("lastModifiedTimestamp: " + e.snapshotMetadata.lastModifiedTimestamp);
trace("playedTime: " + e.snapshotMetadata.playedTime);
trace("progressValue: " + e.snapshotMetadata.progressValue);
trace("uniqueName: " + e.snapshotMetadata.uniqueName);
trace("owner.displayName: " + e.snapshotMetadata.owner.displayName);
trace("gameMetadata.displayName: " + e.snapshotMetadata.gameMetadata.displayName);
toLoadSnapshot(e.snapshotMetadata.uniqueName);
}
By calling the save() method, you must listen to the required events to know the result of the save progress.
function toSaveNewSnapshot():void
{
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_SAVE_STARTED, onSaveStarted);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_SAVE_CONFLICTED, onSaveConflicted);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_SAVE_ENDED, onSaveEnded);
Games.snapshots.save(
"mySave-" + Number(Math.random().toFixed(5)) * 100000,
"save any string but you may find saving JSON Strings more helpful for saving complex game data.",
"description of the save item",
coverImg.bitmapData,
20,
123456,
Snapshots.RESOLUTION_POLICY_MANUAL
);
}
function onSaveStarted(e:GamesEvents):void
{
trace("onSaveStarted: " + e.snapshotName);
}
function onSaveConflicted(e:GamesEvents):void
{
trace("-------------- onSaveConflicted. You should resolve the conflict now using 'resolveConflict' method");
trace("conflict id: " + e.conflictId);
trace("serverSnapshot.metadata.uniqueName " + e.serverSnapshot.metadata.uniqueName);
trace("serverSnapshot.metadata.lastModifiedTimestamp " + e.serverSnapshot.metadata.lastModifiedTimestamp);
trace("serverSnapshot.content " + e.serverSnapshot.content);
trace("-");
trace("conflictingSnapshot.metadata.uniqueName " + e.conflictingSnapshot.metadata.uniqueName);
trace("conflictingSnapshot.metadata.lastModifiedTimestamp " + e.conflictingSnapshot.metadata.lastModifiedTimestamp);
trace("conflictingSnapshot.content " + e.conflictingSnapshot.content);
}
function onSaveEnded(e:GamesEvents):void
{
if(e.status == Games.SUCCESS)
{
trace("onSaveEnded");
}
else if(e.status == Games.FAILURE)
{
trace("onSaveEnded: " + e.msg);
}
}
If the player's device is not connected to a network when your app calls ```save()``, Google Play games services stores the saved game data locally on the device. Upon device re-connection, Google Play games services syncs the locally cached saved game changes to Google's servers.
Loading Saved Games
To retrieve all saved games for the currently signed-in player, call the getList() method and listen to the GamesEvents.SNAPSHOT_GET_LIST event.
By calling the load() method and passing in the name of the selected snapshot, your game saved data will be returned and you must listen to the required events to know when your game snapshot is loaded.
function toLoadSnapshot($snapshotName:String):void
{
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_LOAD_STARTED, onSnapshotLoadStarted);
Games.snapshots.addEventListener(GamesEvents.SNAPSHOT_LOAD_ENDED, onSnapshotLoadEnded);
Games.snapshots.load($snapshotName);
}
function onSnapshotLoadStarted(e:GamesEvents):void
{
trace("onSnapshotLoadStarted: " + e.snapshotName);
}
function onSnapshotLoadEnded(e:GamesEvents):void
{
if(e.status == Games.SUCCESS)
{
trace("serverSnapshot.metadata.uniqueName " + e.serverSnapshot.metadata.uniqueName);
trace("serverSnapshot.metadata.lastModifiedTimestamp " + e.serverSnapshot.metadata.lastModifiedTimestamp);
trace("serverSnapshot.content " + e.serverSnapshot.content);
}
else if(e.status == Games.FAILURE)
{
trace("onSnapshotLoadEnded: " + e.msg);
}
}
Handling saved game conflicts
When using the Saved Games service in your game, it is possible for multiple devices to perform reads and writes on the same saved game. In the event that a device temporarily loses its network connection and later reconnects, this might cause data conflicts whereby the saved game stored on a player's local device is out-of-sync with the remote version stored in Google's servers. The Saved Games services provides a conflict resolution mechanism that presents both sets of conflicting saved games at read-time and lets you implement a resolution strategy that is appropriate for your game.
When Google Play games services detects a data conflict, it notifies your game by this event: GamesEvents.SNAPSHOT_SAVE_CONFLICTED. This event returns two snapshots of your game which are conflicting with each other. Your job is two analyze these snapshots based on your game preferences and then call resolveConflict() passing in the conflictId
and the final content
you wish to use. It is probable that the resolveConflict()
method causes another GamesEvents.SNAPSHOT_SAVE_CONFLICTED
event which you should resolve that again.
DISCRIMINATION: We have copied and when needed has modified the Google documents so it will fit the needs of Adobe Air community. If you wish to see the original documentations, visit here. But if you are interested to do things in Adobe Air, then you are in the right place.