How to make and use a reusable loading screen scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rhyzue

Hi everyone, I’m trying to make a flexible and reusable loading screen with animation.

How it should get used:
1.Current scene loads new scene by calling the following function in Global.gd, a singleton:

func goto_scene(path):
	call_deferred("_deferred_goto_scene", path)
	
func _deferred_goto_scene(path):
	var loadingScene = ResourceLoader.load("res://GUI/LoadingScreen/Loading.tscn").instance()
	current_scene.free()
	current_scene = loadingScreen
	get_tree().get_root().add_child(current_scene)
	get_tree().set_current_scene(current_scene)
	loadingScene.goto_scene(path)

2.This function sets up Loading.tscn, which will load the new scene and display the loading animation.

In my test implementation, I followed this tutorial (Background loading — Godot Engine (stable) documentation in English) to set Loading.tscn up. The code I have for this scene almost exactly follows the tutorial, aside from having a few onready components (progress bar, tween)

Problem is, it takes a looooong time to switch to Loading.tscn. So when the user presses the button to load a new scene, there will be a long freeze. Is there a better way to make this sort of reusable loading screen?

:bust_in_silhouette: Reply From: rakkarage
  • I think you don’t need call_deferred as that will just make it take longer
  • You dont need to load ot instance the scene
  • It looks like this is an infinite loop as goto_scene calls _deferred_goto_scene which again calls goto_scene idk

This seems to work.

extends Node

func goto_scene(path):
	get_tree().change_scene(path)