godot-demo-projects/loading/background_load/background_load.gd

65 lines
1.6 KiB
GDScript3
Raw Normal View History

2018-07-23 00:28:36 +02:00
extends Control
const SIMULATED_DELAY_SEC = 0.1
2018-07-23 00:28:36 +02:00
var thread = null
2018-07-23 00:28:36 +02:00
onready var progress = $Progress
2018-07-23 00:28:36 +02:00
func _thread_load(path):
var ril = ResourceLoader.load_interactive(path)
assert(ril)
2018-07-23 00:28:36 +02:00
var total = ril.get_stage_count()
# Call deferred to configure max load steps.
progress.call_deferred("set_max", total)
2018-07-23 00:28:36 +02:00
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).
2018-07-23 00:28:36 +02:00
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.
2018-07-23 00:28:36 +02:00
res = ril.get_resource()
break
elif err != OK:
# Not OK, there was an error.
2018-07-23 00:28:36 +02:00
print("There was an error loading")
break
# Send whathever we did (or did not) get.
call_deferred("_thread_done", res)
2018-07-23 00:28:36 +02:00
2018-07-23 00:28:36 +02:00
func _thread_done(resource):
assert(resource)
# Always wait for threads to finish, this is required on Windows.
2018-07-23 00:28:36 +02:00
thread.wait_to_finish()
# Hide the progress bar.
2018-07-23 00:28:36 +02:00
progress.hide()
# Instantiate new scene.
2018-07-23 00:28:36 +02:00
var new_scene = resource.instance()
# Free current scene.
2018-07-23 00:28:36 +02:00
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.
2018-07-23 00:28:36 +02:00
get_tree().current_scene = new_scene
2018-07-23 00:28:36 +02:00
progress.visible = false
func load_scene(path):
thread = Thread.new()
thread.start( self, "_thread_load", path)
raise() # Show on top.
2018-07-23 00:28:36 +02:00
progress.visible = true