Skip to content

Commit 22e7476

Browse files
authored
GameState: Persist current scene in global, not scene, state (#2045)
Previously the current scene was persisted as part of the quest state. When you return to Fray's End after completing a trio of Sokobans, all quest state is erased from the saved data, meaning that the current scene path is lost. As a result, if you: - Complete a quest - Complete all three Sokobans & return to Fray's End - Exit the game - Relaunch the game then you were previously offered the chance to Continue. However this would try to change scene to empty string, which fails and reloads the title screen. Fix this in two ways: 1. Persist the current scene as part of the global state, not as part of the quest state. 2. Only offer the chance to continue a saved game if the current scene is known. Add some assertions at relevant points. Resolves #1946
1 parent 5210b65 commit 22e7476

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

scenes/globals/game_state/game_state.gd

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ const INVENTORY_SECTION := "inventory"
3030
const INVENTORY_ITEMS_KEY := "items_collected"
3131
const QUEST_SECTION := "quest"
3232
const QUEST_PATH_KEY := "resource_path"
33-
const QUEST_CURRENTSCENE_KEY := "current_scene"
34-
const QUEST_SPAWNPOINT_KEY := "current_spawn_point"
3533
const QUEST_CHALLENGE_START_KEY := "challenge_start_scene"
3634
const GLOBAL_SECTION := "global"
3735
const GLOBAL_INCORPORATING_THREADS_KEY := "incorporating_threads"
3836
const COMPLETED_QUESTS_KEY := "completed_quests"
37+
const CURRENTSCENE_KEY := "current_scene"
38+
const SPAWNPOINT_KEY := "current_spawn_point"
3939
const LIVES_KEY := "current_lives"
4040
const MAX_LIVES := 2 ** 53
4141
const DEBUG_LIVES := false
@@ -156,7 +156,7 @@ func set_scene(scene_path: String, spawn_point: NodePath = ^"") -> void:
156156
## Set the current spawn point and save it.
157157
func set_current_spawn_point(spawn_point: NodePath = ^"") -> void:
158158
current_spawn_point = spawn_point
159-
_state.set_value(QUEST_SECTION, QUEST_SPAWNPOINT_KEY, current_spawn_point)
159+
_state.set_value(GLOBAL_SECTION, SPAWNPOINT_KEY, current_spawn_point)
160160
_save()
161161

162162

@@ -215,8 +215,8 @@ func _do_set_scene(scene_path: String, spawn_point: NodePath = ^"") -> void:
215215
intro_dialogue_shown = false
216216

217217
current_spawn_point = spawn_point
218-
_state.set_value(QUEST_SECTION, QUEST_CURRENTSCENE_KEY, scene_path)
219-
_state.set_value(QUEST_SECTION, QUEST_SPAWNPOINT_KEY, current_spawn_point)
218+
_state.set_value(GLOBAL_SECTION, CURRENTSCENE_KEY, scene_path)
219+
_state.set_value(GLOBAL_SECTION, SPAWNPOINT_KEY, current_spawn_point)
220220

221221

222222
## Add the [InventoryItem] to the [member inventory].
@@ -341,12 +341,12 @@ func clear() -> void:
341341

342342
## Check if there is persisted state.
343343
func can_restore() -> bool:
344-
return _state.get_sections().size()
344+
return get_scene_to_restore() != ""
345345

346346

347347
## If there is a scene to restore, return it.
348348
func get_scene_to_restore() -> String:
349-
return _state.get_value(QUEST_SECTION, QUEST_CURRENTSCENE_KEY, "")
349+
return _state.get_value(GLOBAL_SECTION, CURRENTSCENE_KEY, "")
350350

351351

352352
## Restore the persisted state.
@@ -361,8 +361,8 @@ func restore() -> Dictionary:
361361
if _state.has_section_key(QUEST_SECTION, QUEST_PATH_KEY):
362362
current_quest = load(_state.get_value(QUEST_SECTION, QUEST_PATH_KEY)) as Quest
363363

364-
var scene_path: String = _state.get_value(QUEST_SECTION, QUEST_CURRENTSCENE_KEY, "")
365-
current_spawn_point = _state.get_value(QUEST_SECTION, QUEST_SPAWNPOINT_KEY, ^"")
364+
var scene_path: String = _state.get_value(GLOBAL_SECTION, CURRENTSCENE_KEY, "")
365+
current_spawn_point = _state.get_value(GLOBAL_SECTION, SPAWNPOINT_KEY, ^"")
366366
incorporating_threads = _state.get_value(
367367
GLOBAL_SECTION, GLOBAL_INCORPORATING_THREADS_KEY, false
368368
)

scenes/globals/scene_switcher/scene_switcher.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func change_to_file_with_transition(
104104
enter_transition: Transition.Effect = Transition.Effect.RIGHT_TO_LEFT_WIPE,
105105
exit_transition: Transition.Effect = Transition.Effect.LEFT_TO_RIGHT_WIPE
106106
) -> void:
107+
assert(scene_path != "")
108+
107109
var err := ResourceLoader.load_threaded_request(scene_path)
108110
if err != OK:
109111
push_error("Failed to start loading %s: %s" % [scene_path, error_string(err)])
@@ -122,6 +124,8 @@ func change_to_packed_with_transition(
122124
enter_transition: Transition.Effect = Transition.Effect.RIGHT_TO_LEFT_WIPE,
123125
exit_transition: Transition.Effect = Transition.Effect.LEFT_TO_RIGHT_WIPE
124126
) -> void:
127+
assert(scene != null)
128+
125129
Transitions.do_transition(
126130
change_to_packed.bind(scene, spawn_point), enter_transition, exit_transition
127131
)
@@ -135,12 +139,16 @@ func reload_with_transition(
135139

136140

137141
func change_to_file(scene_path: String, spawn_point: NodePath = ^"") -> void:
142+
assert(scene_path != "")
143+
138144
var scene: PackedScene = load(scene_path)
139145
if scene:
140146
change_to_packed(scene, spawn_point)
141147

142148

143149
func change_to_packed(scene: PackedScene, spawn_point: NodePath = ^"") -> void:
150+
assert(scene != null)
151+
144152
GameState.clear_per_scene_state()
145153

146154
if get_tree().change_scene_to_packed(scene) == OK:

0 commit comments

Comments
 (0)