Skip to content

SavedGames

Francisco Dias edited this page Apr 6, 2024 · 5 revisions

SavedGames

The Saved Games service gives you a convenient way to save your players' game progression to Google's servers. Your game can retrieve the saved game data to allow returning players to continue a game at their last save point from any device.

The Saved Games service makes it possible to synchronize a player's game data across multiple devices. For example, if you have a game that runs on Android, you can use the Saved Games service to allow a player to start a game on their Android phone, and then continue playing on a tablet without losing any of their progress. This service can also be used to ensure that a player's game play continues from where it left off even if their device is lost, destroyed, or traded in for a newer model.

Functions

The following functions are provided for working with saved games:

Structs

The following structurs are used as output values from the function calls to the GooglePlayServices API:



Back To Top

GooglePlayServices_SavedGames_CommitAndClose

This function requests the Google Play Services API to commit data to a given save slot. The unique identifier of the slot needs to exist meaning that this function will only update already existing files (see GooglePlayServices_SavedGames_CommitNew for creating new ones) but the description, data and coverPath can be changed if needed.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_CommitAndClose(name, description, data, coverPath)
Argument Type Description
name String The unique identifier of the save game slot.
description String The description of the current save slot.
data String A string containing the data you want to save (note you can use json formatted string to store data).
coverPath String The path to the image to be used as a save slot cover image.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_CommitAndClose"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.

Example:

GooglePlayServices_SavedGames_CommitAndClose("slot_1", "Random Description....", json_stringify(struct), coverPath);

The code sample above save the identifier that can be used inside an Social Async Event.

if(async_load[? "type"] == "GooglePlayServices_SavedGames_CommitAndClose")
{
    if(async_load[? "success"])
      show_debug_message("Saved")
    else
      show_debug_message("Not saved :(")
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_CommitNew

This function requests the Google Play Services API to commit data to a given save slot. This function will create a new slot, if you want to update an already existing slot use the GooglePlayServices_SavedGames_CommitAndClose function.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_CommitNew(name, description, data, coverPath)
Argument Type Description
name String The unique identifier of the save game slot.
description String The description of the current save slot.
data String A string containing the data you want to save (note you can use json formatted string to store data).
coverPath String The path to the image to be used as a save slot cover image.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_CommitAndClose"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.
snapshotMetadata String A json formatted string of SnapshotMetadataJSON. This string can be parsed into a struct with the function json_parse. Only available if the task succeeds.

Example:

GooglePlayServices_SavedGames_CommitNew("slot_1", "Random Description....", json_stringify(struct), coverPath);

The code sample above save the identifier that can be used inside an Social Async Event.

if(async_load[? "type"] == "GooglePlayServices_SavedGames_CommitNew")
{
    if(async_load[? "success"])
      show_debug_message("Created");

      var snapshots = json_parse(async_load[?"snapshotMetadata"]);
      // Do something with the snapshots
    else
      show_debug_message("Not created :(")
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_Delete

This function requests the Google Play Services API to delete a given save slot given its unique identifier.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_Delete(name)
Argument Type Description
name String The unique identifier of the save game slot.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_Delete"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.

Example:

GooglePlayServices_SavedGames_Delete("slot_1");

The code sample above save the identifier that can be used inside an Social Async Event.

if(async_load[? "type"] == "GooglePlayServices_SavedGames_Delete")
{
    if(async_load[? "success"])
      show_debug_message("Deleted")
    else
      show_debug_message("Not Deleted :(")
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_DiscardAndClose

This function requests the Google Play Services API to discard changes and close the currently opened save slot.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_DiscardAndClose(name)
Argument Type Description
name String The unique identifier of the save game slot.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_DiscardAndClose"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.

Example:

GooglePlayServices_SavedGames_DiscardAndClose("slot_1")

The code sample above save the identifier that can be used inside an Social Async Event.

if(async_load[? "type"] == "GooglePlayServices_SavedGames_DiscardAndClose")
{
    if(async_load[? "success"])
      show_debug_message("Closed (discarding)")
    else
      show_debug_message("Not closed :(")
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_Load

This function requests the Google Play Services API to query all the save slot metadata of the currently signed-in user.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_Load(forceReload)
Argument Type Description
forceReload Boolean Whether or not the current local cache should be cleared and a new fetch should be performed from the server.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_Load"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.
snapshots String A json formatted string of an array of SnapshotMetadataJSON. This string can be parsed into a struct with the function json_parse. Only available if the task succeeds.

Example:

GooglePlayServices_SavedGames_Load(true)

The code sample above save the identifier that can be used inside an Social Async Event.

if(async_load[? "type"] == "GooglePlayServices_SavedGames_Load")
{
    if(!async_load[?"success"])
       exit

    var snapshots = json_parse(async_load[?"snapshots"]);
    //do something with snapshots
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_Open

This function requests the Google Play Services API to open a given save slot given its unique name identifier.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_Open(name)
Argument Type Description
name String The unique identifier of the save game slot.

Returns:

Real


Triggers:

Social Async Event

Key Type Description
type String The string "GooglePlayServices_SavedGames_Open"
ind Real The id of the request this callback refers to.
success Boolean Whether or not the function request succeeded.
data String The string previously saved to the Google Play Services. Only available if the task succeeds.

Example:

GooglePlayServices_SavedGames_Open("slot_1")

The code sample above save the identifier that can be used inside an Social Async Event

if(async_load[? "type"] == "GooglePlayServices_SavedGames_Open")
if (!async_load[? "success"])
{
    GooglePlayServices_SavedGames_Load(true);

    var data = async_load[? "data"];
    // do something with the data
    return;
}

The code above matches the response against the correct event type and logs the success of the task. For a full usage sample check the provided demo.




Back To Top

GooglePlayServices_SavedGames_ShowSavedGamesUI

This function requests Google Play Services API to open the saved games UI overlay, giving you the ability to see, add (optional) and delete (optional) the save slots.

Note

This function will open an overlay the user can interact with, during this time there are some Async Social events that can be trigger depending on the actions performed by the user. Theses events are showed in the tables below.

This function operates asynchronously, which means that it does not immediately return the requested result. Instead, upon completion of the task, it will trigger the Social Async Event.


Syntax:

GooglePlayServices_SavedGames_ShowSavedGamesUI(title, add, delete, max)
Argument Type Description
title String The text to be used as the title of the overlay.
add Boolean Whether or not the Add slot button should be displayed.
delete Boolean Whether or not the Delete slot button should be displayed.
max Boolean Sets the maximum amount of slots allowed. This can be useful to limit the number of saves you want the player to be able to create.

Returns:

Real


Triggers:

Social Async Event

Triggered whenever an existing slot is selected

Key Type Description
type String The string "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnOpen".
ind Real The id of the request this callback refers to.
snapshotMetadata String A json formatted string of SnapshotMetadataJSON. This string can be parsed into a struct with the function json_parse.

Triggered whenever a new slot is created.

Key Type Description
type String The string "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnNew".
ind Real The id of the request this callback refers to.

Triggered when the dialog is closed.

Key Type Description
type String The string "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnExit".
ind Real The id of the request this callback refers to.

Example:

GooglePlayServices_SavedGames_ShowSavedGamesUI("Save Point", true, true, 3)

The code above will trigger the save games overlay with options to create/delete save slots and with a maximum number of 3 slots. From now on the triggered events can be caught inside an Social Async Event, as follows:

switch(async_load[? "type"])
{
    case "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnNew":
        dialog_ind = get_string_async("Description: ","Slot #0");
        break;

    case "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnOpen":
        var snapshotMeta = json_parse(async_load[? "snapshotMetadata"]);
        var uniqueName = snapshotMeta.uniqueName;
        GooglePlayServices_SavedGames_Open(uniqueName);
        break;

    case "GooglePlayServices_SavedGames_ShowSavedGamesUI_OnExit":
        GooglePlayServices_SavedGames_Load(true);
        break;
}

The code above matches the response against the correct event type and acts accordingly by requesting the user a description for the new slot (if a new slot was created), opens a existinf slot (using GooglePlayServices_SavedGames_Open, if the user selects an existing one) or reloads the slot data (using GooglePlayServices_SavedGames_Load, if the user closes the overaly). For a full usage sample check the provided demo.




Back To Top

SnapshotMetadataJSON

Is a json formatted string that represents a snapshot and its associated metadata.


Member Type Description
coverImageAspectRatio Real The aspect ratio of the cover image for this snapshot, if any.
coverImageUri String An image URI that can be used to load the snapshot's cover image (see GooglePlayServices_UriToPath). Not present if the metadata has no cover image.
description String The description of this snapshot.
deviceName Real The name of the device that wrote this snapshot, if known.
game GameJSON A struct of GameJSON.
lastModifiedTimestamp Real The last time this snapshot was modified, in millis since epoch.
owner PlayerJSON A struct of PlayerJSON.
playedTime Real The played time of this snapshot in milliseconds.
progressValue Real The progress value for this snapshot.
uniqueName String The unique identifier of this snapshot.
hasChangePending Boolean Indicates whether or not this snapshot has any changes pending that have not been uploaded to the server.

Clone this wiki locally