Skip to content

Commit 2b4db5e

Browse files
committed
Refactored world loader to hopefully work more reliably.
1 parent 2995923 commit 2b4db5e

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

scripts/system/world_loader.gd

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ extends Node
55

66
var world_paths:Dictionary = {}
77
var regex:RegEx
8-
var load_check_thread:Thread = Thread.new()
8+
var loading_path:String
9+
var last_load_progress := 0
910

1011

1112
## Called when the loading process begins.
@@ -30,6 +31,28 @@ func _ready():
3031
GameInfo.is_loading = false
3132

3233

34+
func _process(_delta: float) -> void:
35+
if not GameInfo.is_loading:
36+
return
37+
38+
var prog = []
39+
match ResourceLoader.load_threaded_get_status(loading_path, prog):
40+
ResourceLoader.THREAD_LOAD_LOADED:
41+
print("Finishing up...")
42+
var ps := ResourceLoader.load_threaded_get(loading_path) as PackedScene
43+
if not ps:
44+
push_error("Failed to load world at %s" % loading_path)
45+
_abort()
46+
_finish_load.call_deferred(ps)
47+
ResourceLoader.THREAD_LOAD_FAILED, ResourceLoader.THREAD_LOAD_INVALID_RESOURCE:
48+
push_error("Could not load world due to thread loading error.")
49+
_abort()
50+
ResourceLoader.THREAD_LOAD_IN_PROGRESS:
51+
if not last_load_progress == prog[0]:
52+
(func(): load_scene_progess_updated.emit(prog[0])).call_deferred()
53+
last_load_progress = prog[0]
54+
55+
3356
## Load a new world.
3457
func load_world(wid:String) -> void:
3558
print("loading world")
@@ -41,35 +64,23 @@ func load_world(wid:String) -> void:
4164
GameInfo.console_unfreeze()
4265
begin_world_loading.emit()
4366
GameInfo.game_loading.emit(wid)
44-
GameInfo.is_loading = true
4567
await get_tree().process_frame
46-
print("processed frame. Unloading world...")
68+
print("Processed frame. Continuing...")
69+
GameInfo.is_loading = true
70+
#await get_tree().process_frame
71+
#print("processed frame. Unloading world...")
72+
var e:Error = ResourceLoader.load_threaded_request(world_paths[wid], "PackedScene", true)
73+
if not e == OK:
74+
push_error("Load thread error: %d" % e)
75+
_abort()
76+
return
77+
78+
last_load_progress = 0
79+
loading_path = world_paths[wid]
80+
4781
_unload_world()
48-
# Spawn waiting thread
49-
print("spawned waiting thread")
50-
ResourceLoader.load_threaded_request(world_paths[wid], "PackedScene", true)
51-
if load_check_thread.is_started():
52-
load_check_thread.wait_to_finish()
53-
load_check_thread = Thread.new()
54-
print("Atarting thread")
55-
load_check_thread.start(_load_check_thread.bind(world_paths[wid]))
56-
57-
58-
# Side thread
59-
func _load_check_thread(path:String) -> void:
60-
# wait until done
61-
var prog = []
62-
var last_progress = 0
63-
print("waiting for load to finish.")
64-
while not ResourceLoader.load_threaded_get_status(path, prog) == ResourceLoader.THREAD_LOAD_LOADED:
65-
if not last_progress == prog[0]:
66-
(func(): load_scene_progess_updated.emit(prog[0])).call_deferred()
67-
last_progress = prog[0]
68-
print("Finishing up...")
69-
_finish_load.call_deferred(ResourceLoader.load_threaded_get(path) as PackedScene)
7082

7183

72-
# Main thread
7384
func _finish_load(w:PackedScene) -> void:
7485
print("finished loading world")
7586
add_child(w.instantiate())
@@ -84,6 +95,13 @@ func _unload_world():
8495
remove_child(get_child(0))
8596

8697

98+
func _abort() -> void:
99+
# TODO: Crash game?
100+
world_loading_ready.emit()
101+
GameInfo.is_loading = false
102+
GameInfo.game_loaded.emit()
103+
104+
87105
## Searches the worlds directory and caches filepaths, matching them to their name
88106
func _cache_worlds(path:String):
89107
var dir = DirAccess.open(path)

0 commit comments

Comments
 (0)