Skip to content

Saved Games (Snapshots)

Kopfenheim edited this page Jan 31, 2018 · 2 revisions

Functions and callbacks for saving the player's progress in the game to the cloud. Make sure to enable Drive API in the Google API Dashboard

The file is saved to a private section of the player's Google Drive and can only be accessed by the game. This can allow the player to save their game on one device and start playing from where they left off on another supported device.

Providing Images for Save Files

When using the Save Game UI, the _on_play_game_services_saved_game_ready_to_save gives you a suggestedImagePath where you can save any image as a .png. You can then call the requestWriteSnapshot and pass in this same suggestedImagePath to tell the module to load the image from that common location and use it for the save file.

In your GDScript code you can use the following code to take an image stored in the "res://" domain of your project and store it in the program store space on the device:

func saveImageToUserDir(source, destination):
	var img = Image.new()
	img.load(source)
	img.save_png(destination)

If you want to use "res://icon.png" as the save file image then use this function as in the _on_play_game_services_saved_game_ready_to_save callback:

saveImageToUserDir("res://icon.png","user://" + suggestedImagePath)

and call the requestWriteSnapshot after that

gpgs.requestWriteSnapshot(snapshotName, data, "Savin' the game, bruv!", suggestedImagePath)

Functions

  • showSavedGamesUI(String title, boolean allowAddButton, boolean allowDelete, int maxSavedGamesToShow)

    Displays the default Google Play Game Services' Save Game UI. This allows the player to see their previously saved games, choose to create a new save game, overwrite an older saved game, or load a saved game (depending on parameters passed to the function)
    • Parameters
      • title: The title that will be displayed on the Saved Game UI
      • allowAddButton: If true, then the UI will allow the player to create a new saved game and the module assumes that you are displaying this UI to save the game. If false, the UI only allows the player to select a previously saved game and the module assumes that you would want to load any selected saved file.
      • allowDelete: If 'true', the user will have the ability to delete a previous saved file.
      • maxSavedGamesToShow: The Saved Game UI will only show these many of the previously saved files
    • Returns
      • Nothing
  • requestWriteSnapshot(String snapshotName, String data, String description, String imageFileName)

    Write data to a save file.
    • Parameters
      • snapshotName: A unique name given to a save file. It is recommended to use the name provided by the _on_play_game_services_saved_game_ready_to_save callback. However, if you are not using the Save Game UI, then you can also create your own snapshotName as long as it remains unique for a given player.
      • data: The data that you wish to save in the snapshot
      • description: A short description about the save file
      • imageFileName: The name of the image (in the module's "cache" folder) that you wish to use with the save file to give more visual context to the file. You can pass an empty string here if you don't want to use an image for the save file.
    • Returns
      • Nothing
  • requestLoadSnapshot(String snapshotName)

    Asynchronously load data from a snapshot. The function starts the asynchronous process but the actual data will be sent through the _on_play_game_services_saved_game_loaded callback
    • Parameters
      • snapshotName: A unique name given to a save file. It is recommended to use the name provided by the _on_play_game_services_saved_game_ready_to_save callback. However, if you are not using the Save Game UI, then you can also use your own snapshotName.
    • Returns
      • Nothing

Callbacks

Uses the fourth instance ID passed to init() through instanceIDsStr when initializing the module (i.e. {id for Saved Games script})

  • _on_play_game_services_saved_game_loading_started()

    Called when the Save Game UI, which was started with allowAddButton = false, returns with the player having selected a file to load. The loading process is started immediately in the background. This callback allows the game's UI to show some kind of "Loading save file..." message to inform the player.
  • _on_play_game_services_saved_game_loaded(String data, boolean loadedWithoutError)

    Called when the data from the requested save file has been loaded
    • String data: The data loaded from the save file
    • boolean loadedWithoutError: It is true if the data was loaded successully and is false otherwise
  • _on_play_game_services_saved_game_ready_to_save(String snapshotName, String suggestedImagePath)

    Called when the Save Game UI, which was started with allowAddButton = false, returns with the player having selected a file to save. If a new save game is selected by the user then the module will create a unique name for the snapshot and provide it as snapshotName. You can prepare the data to be saved and then call the requestWriteSnapshot() function within this callback.
    • String snapshotName: The name of the snapshot selected by the player or a suggested name for a new save game
    • boolean suggestedImagePath: The location where the module recommends that any image you wish to associate with the save file should be placed. By using this location, the leftover file could be cleared by calling the clearCache() method on the singleton.
  • _on_play_game_services_saved_game_saved(boolean savedWithoutError)

    Called when the data for a save file has been saved
    • boolean suggestedImagePath: It is true if the data was saved successully and is false otherwise