1extends Node
2
3# Existing configured SDK and authenticated player session required.
4# Invoke run_reviewed_action explicitly after reviewing this example.
5# This explicit action reports the shown persisted batch.
6func run_reviewed_action() -> void:
7 if not Horizon.auth.isSignedIn():
8 return
9 var snapshot: Dictionary = await Horizon.statistics.getStatistics()
10 if snapshot.is_empty():
11 push_error(Horizon.statistics.getLastErrorCode())
12 return
13 var pending_path: String = "user://statistics_" + snapshot.userId + ".dat"
14 var pending: Dictionary
15 if FileAccess.file_exists(pending_path):
16 var file := FileAccess.open(pending_path, FileAccess.READ)
17 if file == null:
18 return
19 var saved: Variant = file.get_var()
20 file.close()
21 if not saved is Dictionary:
22 return
23 pending = saved
24 else:
25 pending = {"operationId": Horizon.statistics.createOperationId(),
26 "updates": [{"key": "kills", "value": 3}, {"key": "best_score", "value": 250}]}
27 var file := FileAccess.open(pending_path, FileAccess.WRITE)
28 if file == null:
29 return
30 file.store_var(pending, false) # Variant storage preserves int64 values.
31 file.flush()
32 var saved_ok: bool = file.get_error() == OK
33 file.close()
34 if not saved_ok:
35 return
36 var original_bytes: PackedByteArray = FileAccess.get_file_as_bytes(pending_path)
37 if original_bytes.is_empty():
38 return
39 var result: Dictionary = await Horizon.statistics.updateStatistics(pending.updates, pending.operationId)
40 if result.is_empty():
41 # Keep the file and original ID, including after timeout or OPERATION_EXPIRED.
42 push_error(Horizon.statistics.getLastErrorCode())
43 return
44 # A delayed O1 success cannot remove another action's O2 original.
45 if FileAccess.get_file_as_bytes(pending_path) == original_bytes:
46 DirAccess.remove_absolute(ProjectSettings.globalize_path(pending_path))
47 print(result.values)