diff --git a/scripts/system/world_loader.gd b/scripts/system/world_loader.gd index e81aa8e..97f2aff 100644 --- a/scripts/system/world_loader.gd +++ b/scripts/system/world_loader.gd @@ -5,7 +5,8 @@ extends Node var world_paths:Dictionary = {} var regex:RegEx -var load_check_thread:Thread = Thread.new() +var loading_path:String +var last_load_progress := 0 ## Called when the loading process begins. @@ -30,6 +31,28 @@ func _ready(): GameInfo.is_loading = false +func _process(_delta: float) -> void: + if not GameInfo.is_loading: + return + + var prog = [] + match ResourceLoader.load_threaded_get_status(loading_path, prog): + ResourceLoader.THREAD_LOAD_LOADED: + print("Finishing up...") + var ps := ResourceLoader.load_threaded_get(loading_path) as PackedScene + if not ps: + push_error("Failed to load world at %s" % loading_path) + _abort() + _finish_load.call_deferred(ps) + ResourceLoader.THREAD_LOAD_FAILED, ResourceLoader.THREAD_LOAD_INVALID_RESOURCE: + push_error("Could not load world due to thread loading error.") + _abort() + ResourceLoader.THREAD_LOAD_IN_PROGRESS: + if not last_load_progress == prog[0]: + (func(): load_scene_progess_updated.emit(prog[0])).call_deferred() + last_load_progress = prog[0] + + ## Load a new world. func load_world(wid:String) -> void: print("loading world") @@ -41,35 +64,23 @@ func load_world(wid:String) -> void: GameInfo.console_unfreeze() begin_world_loading.emit() GameInfo.game_loading.emit(wid) - GameInfo.is_loading = true await get_tree().process_frame - print("processed frame. Unloading world...") + print("Processed frame. Continuing...") + GameInfo.is_loading = true + #await get_tree().process_frame + #print("processed frame. Unloading world...") + var e:Error = ResourceLoader.load_threaded_request(world_paths[wid], "PackedScene", true) + if not e == OK: + push_error("Load thread error: %d" % e) + _abort() + return + + last_load_progress = 0 + loading_path = world_paths[wid] + _unload_world() - # Spawn waiting thread - print("spawned waiting thread") - ResourceLoader.load_threaded_request(world_paths[wid], "PackedScene", true) - if load_check_thread.is_started(): - load_check_thread.wait_to_finish() - load_check_thread = Thread.new() - print("Atarting thread") - load_check_thread.start(_load_check_thread.bind(world_paths[wid])) - - -# Side thread -func _load_check_thread(path:String) -> void: - # wait until done - var prog = [] - var last_progress = 0 - print("waiting for load to finish.") - while not ResourceLoader.load_threaded_get_status(path, prog) == ResourceLoader.THREAD_LOAD_LOADED: - if not last_progress == prog[0]: - (func(): load_scene_progess_updated.emit(prog[0])).call_deferred() - last_progress = prog[0] - print("Finishing up...") - _finish_load.call_deferred(ResourceLoader.load_threaded_get(path) as PackedScene) -# Main thread func _finish_load(w:PackedScene) -> void: print("finished loading world") add_child(w.instantiate()) @@ -84,6 +95,13 @@ func _unload_world(): remove_child(get_child(0)) +func _abort() -> void: + # TODO: Crash game? + world_loading_ready.emit() + GameInfo.is_loading = false + GameInfo.game_loaded.emit() + + ## Searches the worlds directory and caches filepaths, matching them to their name func _cache_worlds(path:String): var dir = DirAccess.open(path)