Change scene to file on Control scene

Godot Version

v4.1.3.stable.official [f06b6836a]

Question

Is there a way to add something similar to SceneTree.change_scene_to_file() on Control class? I’m trying to switch it to another scene but method change_scene_to_file doesn’t exist in base ‘Control’, the method is in ‘SceneTree’.

I’m aware I can use get_tree().change_scene_to_file(), but the issue is this would change the entire scene tree. I want to change a specific scene in the scene tree regardless of its class.

Also trying to change the script heritance (extends Something to extends SceneTree) outputs error; "Script inherits from native type ‘SceneTree’, so it can’t be assigned to an object of type: ‘ObjectTypeName’ ".

Use remove,queue and add child

Here’s an example

Here’s the thing, I want to change a specific scene in the scene tree, I can use get_tree().change_scene_to_file(), but the issue is this would change the entire scene tree. Is there a way of calling change_scene_to_file on a specific scene regardless of its class?

I figured out a solution (kind of), feel free to correct me or add something:

var instantiated_new_scene = preload("res://level_selection_menu.tscn").instantiate()

func change_scene():
	get_tree().root.get_node("The node path").add_child(instantiated_new_scene)
	get_tree().root.remove_child(get_node("The node path"))
#	Something like that, not sure about the syntax

Though I still have a few questions. Does get_tree().root target the first node “World” in this mockup hierarchy?

↓ World
↓ Level
→ Player

similar to what i’ve shown (thru the link), basically you remove and queue_free the scene you are at now, then instantiate and add as child, you dont have to get root to make it possible, offers more flexibility

you mean like this?
image

Hi,

The root of your scene tree is NOT the root of the scene your playing, because every time you run a game in Godot, it sets a viewport as the root, which is needed to see everything happening on the screen!

You can see this if you run your game and see the “remote” part

image