The idea is to switch to a scene that runs a loading animation and having a thread to set up the next scene. once the thread is finished you remove the loading screen scene and add the new scene created by the thread into the tree. The code below is something i just typed up for you to get an idea of how to go about this, I didn’t actually test this code.
onready var load_stuff = Thread.new()
var current_scene= #your scene here
#new_scene here is a node and loading is a boolean
func goto_scene(new_scene,loading):
current_scene.queue_free()
current_scene=new_scene
# Add new scene as child of root
get_tree().get_root().call_deferred("add_child",current_scene)
# optional, to make it compatible with the SceneTree.change_scene() API
get_tree().set_current_scene( current_scene )
# if we are using the loading scene then we want a thread to prep next scene
if(loading):
load_stuff.start(self,"prep_scene")
func prep_scene():
#your code for setting up the next scene goes here
var s = ResourceLoader.load(path)
var scene= s.instance()
#make sure if add_child function is used anywhere during the thread you use call deferred since add_child() is not very thread safe, look at the goto_scene for an example of this.
#once the new scene is prepared we can switch to it, also set false since we aren't doing the loading screen again
goto_scene(scene,false)
load_stuff.call_deferred('wait to finish')
the first argument should be a node, right ? But if the scene it’s in is not loaded yet, how can it be targetted ? Should I put “res://scenes/ingame.tscn” instead ? (none seem to work)
and how the goto_scene and prep_scene work together ?
The way I understand it, is :
1/ running goto_scene with loading = 1
2/ running prep_scene
3/ running goto_scene with loading = 0