Loading scene and passing a string

Godot Version

4.2.2

Question

I’m trying to create a loading screen using ResourceLoader.load_threaded_request(“scene_name”) but I can’t for the life of me figure out how I would customize the “scene_name” so I can reuse this loading scene for level transitions. My scene manager (autoload) changes scene to the loading scene, but I need to also provide the path for the new scene and that’s where I’m stuck. I’ve got it hardcoded in the loading scene. I’ve tried a few things but can’t get to a solution.

A solution I can think of goes like this (just adding the new code):

func _ready():
    set_process(false)

func change_scene_to(scene: str) -> void:
    scene_name = scene
    ResourceLoader.load_threaded_request(scene_name)
    set_process(true)

func load_new_scene():
    get_tree().change_scene_to_packed()
    set_process(false)

You would basically need to call the function change_scene_to with the path to the new_scene, and because set_process is set to false at the start of the script the transition will only work when you call this method.

I hope this works for you :smile:

I’ve actually tried that but from my SceneManager, I call

get_tree().change_scene_to_packed(LOADING_SCENE)

and when I tried to access the new scene with

get_tree().current_scene

it’s always null. Been reading about it and something to do with the change scene doesn’t automatically set to the current scene and has some kind of parent node intended by design. I was trying to set this up for later down the line when loading actually takes a bit of time. Here I’m just artificially slowing it down with a tween. I think I’ll try add the scenes as a child to my SceneManager (autoload) instead :weary: :weary: