From 667aa38cda6175f1b1745fbea9e99bc8e6d41132 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Sun, 7 Sep 2025 01:45:25 -0600 Subject: [PATCH] Avoid adding loaded scene to tree until all loading is finished. --- src/game/editor_entrypoint/editor_entrypoint.gd | 2 +- src/game/game.gd | 11 +++++++++++ src/util/loading_tools.gd | 3 +++ src/world/world.tscn | 1 - 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/game/editor_entrypoint/editor_entrypoint.gd b/src/game/editor_entrypoint/editor_entrypoint.gd index d9dbca2..a4f5490 100644 --- a/src/game/editor_entrypoint/editor_entrypoint.gd +++ b/src/game/editor_entrypoint/editor_entrypoint.gd @@ -3,6 +3,6 @@ extends Node @export_file("*.tscn") var initial_scene := "res://src/world/world.tscn" -func _ready() -> void: +func _init() -> void: print("Starting from editor-only entrypoint.") Game.instance.queue_scene(initial_scene) diff --git a/src/game/game.gd b/src/game/game.gd index d01dc1e..88fc245 100644 --- a/src/game/game.gd +++ b/src/game/game.gd @@ -4,6 +4,7 @@ class_name Game extends Node @export_file("*.tscn") var start_scene: String var _loading_resources: Dictionary[String, Promise] = {} +var _queued_content: Node @onready var content: Node = %Content @onready var loading_screen: Control = %LoadingScreen @@ -89,6 +90,10 @@ func _unload_content() -> void: func _finish_scene_load(scene_instance: Node) -> void: + _queued_content = scene_instance + + +func _add_scene_content(scene_instance: Node) -> void: # Unpause in case the previous scene was paused. get_tree().paused = false # Reset time scale in case it's been changed. @@ -115,5 +120,11 @@ func _process(_delta: float) -> void: # Continue loading pass + if not _loading_resources: + # Delay adding queued scene to the tree until loading is finished + if _queued_content: + _add_scene_content(_queued_content) + _queued_content = null + if not _loading_resources: loading_screen.visible = false diff --git a/src/util/loading_tools.gd b/src/util/loading_tools.gd index 6263f45..782413d 100644 --- a/src/util/loading_tools.gd +++ b/src/util/loading_tools.gd @@ -9,14 +9,17 @@ static func _load_world(level: PackedScene, save: SaveState = null) -> void: world.save_state = save world.initial_level = level + print_debug("Loading world") Game.instance.queue_scene(WORLD_SCENE).then(finish_load) static func load_level(level_path: String, save: SaveState = null) -> void: + print_debug("Loading level from ", level_path) var chain_load := func(level: PackedScene) -> void: LoadingTools._load_world(level, save) Game.instance.queue_load(level_path).then(chain_load) static func load_save(save_path: String) -> void: + print_debug("Loading save from ", save_path) var chain_load := func(save: SaveState) -> void: load_level(save.level_path, save) Game.instance.queue_load(save_path, ResourceLoader.CACHE_MODE_REPLACE_DEEP).then(chain_load) diff --git a/src/world/world.tscn b/src/world/world.tscn index cc69730..3108ce2 100644 --- a/src/world/world.tscn +++ b/src/world/world.tscn @@ -13,7 +13,6 @@ volume_db = -14.0 [node name="World" type="Node"] script = ExtResource("1_1k4gi") -pause_enabled = null manager = ExtResource("2_5kmgb") spook_manager = ExtResource("3_l0av5") pause_music_effect = SubResource("AudioEffectAmplify_5kmgb")