41 lines
1.4 KiB
GDScript3
41 lines
1.4 KiB
GDScript3
|
|
extends Node
|
||
|
|
## Saves (Autoload) — Spielstand schreiben und lesen (docs/engine.md §7).
|
||
|
|
##
|
||
|
|
## Format: JSON unter user://, mit `save_version`. Gespeichert werden nur
|
||
|
|
## Eingaben; abgeleitete Werte (RK, Angriffsbonus) rechnet Game neu.
|
||
|
|
## Eine unbekannte Version wird nicht stillschweigend geladen.
|
||
|
|
|
||
|
|
const VERSION := 1
|
||
|
|
const ORDNER := "user://saves"
|
||
|
|
|
||
|
|
func pfad(slot: int = 0) -> String:
|
||
|
|
return "%s/slot_%d.json" % [ORDNER, slot]
|
||
|
|
|
||
|
|
func write(slot: int = 0) -> bool:
|
||
|
|
DirAccess.make_dir_recursive_absolute(ORDNER)
|
||
|
|
var datei := FileAccess.open(pfad(slot), FileAccess.WRITE)
|
||
|
|
if datei == null:
|
||
|
|
push_error("Saves: kann %s nicht schreiben (%s)" % [pfad(slot), FileAccess.get_open_error()])
|
||
|
|
return false
|
||
|
|
datei.store_string(JSON.stringify(Game.to_dict(), "\t"))
|
||
|
|
datei.close()
|
||
|
|
return true
|
||
|
|
|
||
|
|
func read(slot: int = 0) -> Dictionary:
|
||
|
|
if not FileAccess.file_exists(pfad(slot)):
|
||
|
|
push_error("Saves: %s fehlt" % pfad(slot))
|
||
|
|
return {}
|
||
|
|
var wert: Variant = JSON.parse_string(FileAccess.get_file_as_string(pfad(slot)))
|
||
|
|
if typeof(wert) != TYPE_DICTIONARY:
|
||
|
|
push_error("Saves: %s ist kein gültiges JSON" % pfad(slot))
|
||
|
|
return {}
|
||
|
|
var d: Dictionary = wert
|
||
|
|
var version := int(d.get("save_version", 0))
|
||
|
|
if version != VERSION:
|
||
|
|
push_error("Saves: Version %d, erwartet %d — Migration fehlt" % [version, VERSION])
|
||
|
|
return {}
|
||
|
|
return d
|
||
|
|
|
||
|
|
func exists(slot: int = 0) -> bool:
|
||
|
|
return FileAccess.file_exists(pfad(slot))
|