There is a better way to do this with a loading screen, just in case the scene takes a while to load. You don’t want people playing your game and think that it crashed!
var loading_target_value = 0
onready var buttons = $UI/Buttons #This would be the parent node to your welcome screen
onready var loading = $UI/Loading #The parent node to a ProgressBar node
onready var loading_progress = $UI/Loading/ProgressBar #ProgressBar node
func interactive_load(loader):
print("starting...")
loading.show()
buttons.hide()
while true:
var status = loader.poll()
if status == OK:
loading_target_value = (loader.get_stage() * 100) / loader.get_stage_count()
continue
elif status == ERR_FILE_EOF:
loading_target_value = 100
loading_done_timer.start()
break
else:
print("Error while loading level: " + str(status))
buttons.show()
loading.hide()
break
func loading_done(loader):
loading_thread.wait_to_finish()
replace_main_scene(loader.get_resource())
res_loader = null
# Weirdly, "res_loader = null" is needed as otherwise
# loading the resource again is not possible.
print("done")
func replace_main_scene(resource):
change_scene(resource)
func change_scene(resource : Resource):
var node = resource.instance()
for child in get_children():
remove_child(child)
child.queue_free()
add_child(node)
func _process(_delta):
if loading_progress != null:
loading_progress.value = lerp(loading_progress.value,loading_target_value,.1) + 1
func onPlaypressed():
var path = "res://Escenas/Main.tscn" #The scene you want to load
res_loader = ResourceLoader.load_interactive(path)
loading_thread = Thread.new()
#warning-ignore:return_value_discarded
loading_thread.start(self, "interactive_load", res_loader)
func _on_LoadTimer_timeout():
loading_done(res_loader)
It worked really well for my project, especially since the scene I’m loading is REALLY big.