Skip to content

Commit

Permalink
Emit null on loading a Snapshot that was not found
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Ibáñez Sánchez <jacobibanez@jacobibanez.com>
  • Loading branch information
Iakobs committed Oct 10, 2024
1 parent 36230dd commit dd97d23
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v1.8.3
### Return null when Snapshot is not found
Ass suggested by @TheSkyOne in [this issue](https://github.com/Iakobs/godot-play-game-services/issues/38), when calling to `SnapshotClient.load_game()` and the snapshot is not found, the `game_loaded` signal will now return a null, instead of not being emitted at all as before.

## v1.8.2
### Add origin field to SnapshotConflict object
As suggested by @RProduction in [this issue](https://github.com/Iakobs/godot-play-game-services/issues/35), I added an `origin` field to the `SnapshotConflict` object coming in the `SnapshotClient.conflict_emitted` signal. This field value is either `SAVE` or `LOAD`, indicating what method originally triggered the Snapshot conflict.
Expand Down
4 changes: 4 additions & 0 deletions plugin/demo/scenes/snapshots/Snapshots.gd
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func _connect_save_game_data() -> void:
)
SnapshotsClient.game_loaded.connect(
func(snapshot: SnapshotsClient.Snapshot):
if snapshot == null:
print("Snapshot not found!")
return

_reset_save_game_data()

var metadata = snapshot.metadata
Expand Down
7 changes: 5 additions & 2 deletions plugin/export_scripts_template/autoloads/snapshots_client.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ signal game_saved(is_saved: bool, save_data_name: String, save_data_description:
## a saved game in the window presented after calling the [method show_saved_games]
## method.[br]
## [br]
## [param snapshot]: The loaded snapshot.
## [param snapshot]: The loaded snapshot, or null if the snapshot wasn't found.
signal game_loaded(snapshot: Snapshot)

## Signal emitted after saving or loading a game, if a conflict is found.[br]
Expand All @@ -38,7 +38,10 @@ func _ready() -> void:
)
GodotPlayGameServices.android_plugin.gameLoaded.connect(func(json_data: String):
var safe_dictionary := GodotPlayGameServices.json_marshaller.safe_parse_dictionary(json_data)
game_loaded.emit(Snapshot.new(safe_dictionary))
if safe_dictionary.is_empty():
game_loaded.emit(null)
else:
game_loaded.emit(Snapshot.new(safe_dictionary))
)
GodotPlayGameServices.android_plugin.conflictEmitted.connect(func(json_data: String):
var safe_dictionary := GodotPlayGameServices.json_marshaller.safe_parse_dictionary(json_data)
Expand Down
2 changes: 2 additions & 0 deletions plugin/export_scripts_template/marshalling/json_marshaller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func safe_parse_dictionary(json_dictionary: String) -> Dictionary:
var data_received = _json.data
if typeof(data_received) == TYPE_DICTIONARY:
return data_received
elif typeof(data_received) == TYPE_NIL:
return {}
else:
printerr("Unexpected data received from JSON Dictionary:\n%s" % json_dictionary)
else:
Expand Down
2 changes: 1 addition & 1 deletion plugin/export_scripts_template/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="GodotPlayGameServices"
description="A Godot 4.3 plugin for Google Play Game Services"
author="Jacob Ibáñez Sánchez"
version="1.8.2"
version="1.8.3"
script="export_plugin.gd"
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ object SnapshotSignals {
* This signal is emitted when calling the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.loadGame] method
* or after selecting a saved game in the window opened by the [com.jacobibanez.plugin.android.godotplaygameservices.GodotAndroidPlugin.showSavedGames] method.
*
* @return A [Dictionary] representing a [com.google.android.gms.games.snapshot.Snapshot](https://developers.google.com/android/reference/com/google/android/gms/games/snapshot/Snapshot).
* @return A [Dictionary] representing a [com.google.android.gms.games.snapshot.Snapshot](https://developers.google.com/android/reference/com/google/android/gms/games/snapshot/Snapshot) or null if the Snapshot wasn't found.
*/
val gameLoaded = SignalInfo("gameLoaded", String::class.java)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.core.app.ActivityCompat
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.games.PlayGames
import com.google.android.gms.games.SnapshotsClient
import com.google.android.gms.games.SnapshotsClient.EXTRA_SNAPSHOT_METADATA
Expand Down Expand Up @@ -33,6 +34,7 @@ class SnapshotsProxy(
private val tag = SnapshotsProxy::class.java.simpleName

private val showSavedGamesRequestCode = 9010
private val snapshotNotFoundCode = 26570

fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == showSavedGamesRequestCode && resultCode == Activity.RESULT_OK) {
Expand Down Expand Up @@ -116,10 +118,19 @@ class SnapshotsProxy(
)
}
} else {
val exception = task.exception
Log.e(
tag,
"Error while opening Snapshot $fileName for loading. Cause: ${task.exception}"
"Error while opening Snapshot $fileName for loading. Cause: $exception"
)
if (exception is ApiException && exception.statusCode == snapshotNotFoundCode) {
emitSignal(
godot,
GODOT_PLUGIN_NAME,
gameLoaded,
Gson().toJson(null)
)
}
}
}
}
Expand Down

0 comments on commit dd97d23

Please sign in to comment.