From 10634bcd81eb39ba3501bcff7f3c6fc6f2dbfab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20Ib=C3=A1=C3=B1ez=20S=C3=A1nchez?= Date: Sat, 2 Mar 2024 15:53:01 +0100 Subject: [PATCH] Add extra parameter to loadGame method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jacob Ibáñez Sánchez --- CHANGELOG.md | 3 +++ .../export_scripts_template/autoloads/snapshots_client.gd | 7 ++++--- .../android/godotplaygameservices/GodotAndroidPlugin.kt | 4 +++- .../godotplaygameservices/snapshots/SnapshotsProxy.kt | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aed5de..5969def 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The [loadPlayerCenteredScores](https://developers.google.com/android/reference/c ### Fix crash when loading non existing save game When calling the `loadGame` method with a non existing file name, the app crashed. This is fixed now, the app just prints a log with the error and continues execution. +### Add new parameter to loadGame method +Added the `createIfNotFound` parameter to the `loadGame` method, with a default value of `false` to not break backwards compatibility. This parameter creates a new snapshot if the file name does not exist. + ## v1.5.0 ### Order of autoloads The autoloads where causing errors on first launch of the project, due to the load order and dependencies between them. The load order has now been fixed to avoid this errors. Also, the plugin is now disabled by default in the demo project. Look at the [demo project documentation](https://github.com/Iakobs/godot-play-game-services/tree/main/plugin/demo) for further info. diff --git a/plugin/export_scripts_template/autoloads/snapshots_client.gd b/plugin/export_scripts_template/autoloads/snapshots_client.gd index 29b844f..f608b95 100644 --- a/plugin/export_scripts_template/autoloads/snapshots_client.gd +++ b/plugin/export_scripts_template/autoloads/snapshots_client.gd @@ -88,10 +88,11 @@ func save_game( ## [br] ## This method emits the [signal game_loaded] signal.[br] ## [br] -## [param fileName]: The name of the save file. Must be between 1 and 100 non-URL-reserved charactes (a-z, A-Z, 0-9, or the symbols "-", ".", "_", or "~"). -func load_game(file_name: String) -> void: +## [param fileName]: The name of the save file. Must be between 1 and 100 non-URL-reserved charactes (a-z, A-Z, 0-9, or the symbols "-", ".", "_", or "~").[br] +## [param create_if_not_found]: False by default. If true, the snapshot will be created if one cannot be found. +func load_game(file_name: String, create_if_not_found := false) -> void: if GodotPlayGameServices.android_plugin: - GodotPlayGameServices.android_plugin.loadGame(file_name) + GodotPlayGameServices.android_plugin.loadGame(file_name, create_if_not_found) ## Loads the list of [SnapshotMetadata] of the current signed in player.[br] ## [br] diff --git a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt index 8bfc786..4d79248 100644 --- a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt +++ b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/GodotAndroidPlugin.kt @@ -423,9 +423,11 @@ class GodotAndroidPlugin(godot: Godot) : GodotPlugin(godot) { * * @param fileName The name of the save file. Must be between 1 and 100 non-URL-reserved * characters (a-z, A-Z, 0-9, or the symbols "-", ".", "_", or "~"). + * @param createIfNotFound False by default. If true, the snapshot will be created if one cannot be found. */ @UsedByGodot - fun loadGame(fileName: String) = snapshotsProxy.loadGame(fileName) + fun loadGame(fileName: String, createIfNotFound: Boolean = false) = + snapshotsProxy.loadGame(fileName, createIfNotFound) /** * Loads the list of [com.google.android.gms.games.snapshot.SnapshotMetadata](https://developers.google.com/android/reference/com/google/android/gms/games/snapshot/SnapshotMetadata) diff --git a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt index 3b6b79b..55f8035 100644 --- a/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt +++ b/plugin/src/main/java/com/jacobibanez/plugin/android/godotplaygameservices/snapshots/SnapshotsProxy.kt @@ -36,7 +36,7 @@ class SnapshotsProxy( if (intent.hasExtra(EXTRA_SNAPSHOT_METADATA)) { val snapshotMetadata = intent.extras ?.get(EXTRA_SNAPSHOT_METADATA) as SnapshotMetadata - loadGame(snapshotMetadata.uniqueName) + loadGame(snapshotMetadata.uniqueName, false) } } } @@ -93,9 +93,9 @@ class SnapshotsProxy( } } - fun loadGame(fileName: String) { + fun loadGame(fileName: String, createIfNotFound: Boolean) { Log.d(tag, "Loading snapshot with name $fileName.") - snapshotsClient.open(fileName, false, RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED) + snapshotsClient.open(fileName, createIfNotFound, RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED) .addOnCompleteListener { task -> if (task.isSuccessful) { val dataOrConflict = task.result