-
Notifications
You must be signed in to change notification settings - Fork 0
/
background_load.gd
executable file
·70 lines (53 loc) · 1.66 KB
/
background_load.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
extends Control
const SIMULATED_DELAY_SEC = 0.1
var thread = null
onready var progress = $Progress
onready var space := $space
func _thread_load(path):
var ril = ResourceLoader.load_interactive(path)
assert(ril)
var total = ril.get_stage_count()
# Call deferred to configure max load steps.
progress.call_deferred("set_max", total)
var res = null
while true: #iterate until we have a resource
# Update progress bar, use call deferred, which routes to main thread.
progress.call_deferred("set_value", ril.get_stage())
# Simulate a delay.
OS.delay_msec(int(SIMULATED_DELAY_SEC * 1000.0))
# Poll (does a load step).
var err = ril.poll()
# If OK, then load another one. If EOF, it' s done. Otherwise there was an error.
if err == ERR_FILE_EOF:
# Loading done, fetch resource.
res = ril.get_resource()
break
elif err != OK:
# Not OK, there was an error.
print("There was an error loading")
break
# Send whathever we did (or did not) get.
call_deferred("_thread_done", res)
func _thread_done(resource):
assert(resource)
# Always wait for threads to finish, this is required on Windows.
thread.wait_to_finish()
# Hide the progress bar.
progress.hide()
# Instantiate new scene.
var new_scene = resource.instance()
# Free current scene.
get_tree().current_scene.free()
get_tree().current_scene = null
# Add new one to root.
get_tree().root.add_child(new_scene)
# Set as current scene.
get_tree().current_scene = new_scene
space.visible = false
progress.visible = false
func load_scene(path):
thread = Thread.new()
thread.start( self, "_thread_load", path)
raise() # Show on top.
space.visible = true
progress.visible = true