#225: save the run, and offer to resume it

JSON at user://savegame.json rather than a binary resource: a save that can
be opened in a text editor is diagnosable from a bug report, and
ResourceSaver would tie the file's shape to GDScript class layout, which is
the thing that keeps changing.

The run is not plain data, which is the actual work here. A tagged codec
carries Vector2, Vector2i, PackedStringArray and the Inventory objects
nested inside `buildings`. explored_chunks is keyed by Vector2i and JSON has
no such key, so every key carries its own type prefix ("v2i:3,-4") - without
that a planet id could come back as a chunk coordinate.

WHAT is saved is an explicit list of 30 fields, not a dump of GameState's
58 vars. A dump would carry transient junk - which pointer last touched the
screen, whether a dialog holds the camera - and would break silently on
every rename. The cost is that new progression must be added to the list;
the benefit is that forgetting is a missing feature, not a corrupt save.

Versioning, as the ticket asked. MIGRATIONS walks a save up one version at a
time; MIN_SUPPORTED_VERSION is how a break too messy to migrate gets
DECLARED rather than bodged. A save that cannot be walked forward is refused
with a player-readable reason, because a save that loads subtly WRONG is
worse than one that refuses and says so. Adding a field needs no bump: it
arrives missing from old saves and keeps its new-game value (case F).

The menu offers Continue only when there is something to offer. A save this
build CANNOT open is still shown, disabled, with the reason underneath -
silently hiding someone's run reads as lost rather than incompatible.

Autosave every 30s, plus on close and on Android's app-pause, since the
phone kills backgrounded apps with no warning. Skipped during the prologue.

That autosave writes to the same user:// path every test that loads
Main.tscn shares, so it could make Continue appear and shift the menu stack
under test_main_menu depending on what ran first. That test clears the save
before building the menu.

Verified by breaking it: neutering the key decoder fails with "chunk keys
came back as [v2i:-11,0, v2i:3,-4]".

Still absent, deliberately: no save-on-demand button, and no route back to
the menu from in-game, so a resume currently needs a relaunch.

175/175.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019AJMdB7iuHuLFZM1aBu3GD
This commit is contained in:
Claude 2026-09-09 21:32:23 +02:00
commit fb3587173d
7 changed files with 634 additions and 1 deletions

View file

@ -102,6 +102,14 @@ var keyboard_seen: bool = false
var keyboard_steering: bool = false
# Seconds between automatic saves. Long enough that writing the file is
# never felt, short enough that a lost run is an annoyance rather than an
# evening.
const AUTOSAVE_INTERVAL: float = 30.0
var autosave_countdown: float = AUTOSAVE_INTERVAL
func _ready() -> void:
# Most visuals still animate per rendered frame. The main/surface scenes opt
# their roots out of global interpolation, then opt their physics-follow
@ -150,6 +158,12 @@ func _held_pivot_direction() -> float:
func _notification(what: int) -> void:
if what == NOTIFICATION_UNPAUSED:
_forget_released_keys()
# #225: the phone is where this is played, and Android kills an app in
# the background without warning - so the run is written on the way out
# rather than only on a timer.
elif what == NOTIFICATION_WM_CLOSE_REQUEST \
or what == NOTIFICATION_APPLICATION_PAUSED:
autosave()
func _forget_released_keys() -> void:
@ -296,7 +310,22 @@ func _on_joystick_direction(direction: Vector2) -> void:
fleet.stop()
# Written on a timer as well as on the way out: a crash or a battery pull
# gives no notification at all, and losing half an hour is what makes a save
# system feel absent even when it is there.
func autosave() -> void:
if GameState.intro_active:
# Nothing worth keeping yet, and saving mid-prologue would resume
# into a run that has not been set up.
return
SaveGame.save(GameState)
autosave_countdown = AUTOSAVE_INTERVAL
func _process(delta: float) -> void:
autosave_countdown -= delta
if autosave_countdown <= 0.0:
autosave()
pivot_cw_tap_cooldown_left = maxf(pivot_cw_tap_cooldown_left - delta, 0.0)
pivot_ccw_tap_cooldown_left = maxf(pivot_ccw_tap_cooldown_left - delta, 0.0)
_update_pivot(delta)