In my game I have a menu where I select different levels which are each different scenes. Going to the scenes causes the game to freeze while it loads though, so I followed the documentation's tutorial for the background loader (https://docs.godotengine.org/en/stable/tutorials/io/background_loading.html).
I have a loading bar that updates with the background loading progress and that works correctly, but it still freezes before the scene actually changes and I can't figure out why.
Here is the relevant code I'm using:
To start the loading:
func goto_level(path):
score = 0
call_deferred("_deferred_goto_level", path)
func _deferred_goto_level(path):
if use_loader == true:
loader = ResourceLoader.load_interactive(path)
if loader == null:
print("error happened")
return
set_process(true)
wait_frames = 1
To process the loading:
func _process(delta):
if loader == null:
set_process(false)
return
if wait_frames > 0:
wait_frames -= 1
return
var err = loader.poll()
if err == ERR_FILE_EOF:
var resource = loader.get_resource()
loader = null
set_new_level(resource)
elif err == OK:
var progress = round(float(loader.get_stage()) / loader.get_stage_count() * 30)
loading_bar.value = progress
else:
print("error while loading")
loader = null
Then to set the scene after it's loaded:
func set_new_level(scene_resource):
current_level.free()
current_level = scene_resource.instance()
call_deferred("add_child", current_level)
The tutorial had a while loop in the process function but that was even worse and resulted in the game freezing for the entire loading and the loading bar not updating, not just when the scene is added.