extends Node @export var scenes : Array[Scene] signal scene_loaded(scene : Scene) signal scene_node_ready(scene : Scene) var actual_scene = null var loading_scene = false var generating_node = false var next_scene_node : Node @onready var current_scene_node : Node = get_tree().root.get_children().back() func search_scenes(scene_id : String) -> Scene: var scene_pos : int = scenes.find_custom( func (s : Scene): return s.scene_id == scene_id ) if scene_pos == -1: return null else : return scenes[scene_pos] func change_to_scene_id(scene_id : String, with_loading = true): var scene = search_scenes(scene_id) if not scene: printerr("Scene %s not found" % scene_id) return change_to_scene(scene, with_loading) func change_to_scene(scene : Scene, with_loading = true): if loading_scene or generating_node: await scene_node_ready actual_scene = scene loading_scene = true var scene_path_to_load = scene.scene_path ResourceLoader.load_threaded_request(scene_path_to_load) LoadingScreen.loading_text = "LOADING_SCENE" var scene_to_hide = current_scene_node if with_loading: await LoadingScreen.show_loading_screen() if scene_to_hide != null and scene_to_hide.has_method("hide"): scene_to_hide.hide() if loading_scene: await scene_loaded next_scene_node = ResourceLoader.load_threaded_get(scene_path_to_load).instantiate() if next_scene_node.has_method("hide"): next_scene_node.hide() get_tree().root.add_child(next_scene_node) generating_node = true if scene.need_terrain_generated: LoadingScreen.loading_text = "GENERATING_TERRAIN" await scene_node_ready if current_scene_node: current_scene_node.queue_free() current_scene_node = next_scene_node if current_scene_node.has_method("show"): current_scene_node.show() Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if scene.mouse_captured else Input.MOUSE_MODE_VISIBLE GameInfo.update_inputs() if actual_scene.need_to_be_saved: GameInfo.game_data.last_game_scene = scene GameInfo.save_game_data() if with_loading: LoadingScreen.hide_loading_screen() func _process(_delta): if loading_scene: var progress = [] var load_status := ResourceLoader.load_threaded_get_status(actual_scene.scene_path, progress) LoadingScreen.loading_value = progress[0] if load_status == ResourceLoader.THREAD_LOAD_LOADED: loading_scene = false scene_loaded.emit(actual_scene) if load_status == ResourceLoader.THREAD_LOAD_FAILED or load_status == ResourceLoader.THREAD_LOAD_INVALID_RESOURCE: printerr() elif generating_node: if next_scene_node is Region: LoadingScreen.loading_value = next_scene_node.generated_value if next_scene_node.is_generated: generating_node = false scene_node_ready.emit() elif next_scene_node.is_node_ready(): generating_node = false scene_node_ready.emit(actual_scene)