Godot Version
4.3
Question
I’m trying to switch between scenes in Godot 4.3 using a main menu as the root scene. When I load a new scene (e.g., map_b_scene), I queue free the main menu (self.queue_free()) and dynamically add the new scene to the root. However, when I try to access a node in the newly loaded scene using get_node() (with a path like (‘/root/’ + get_tree().current_scene.name + ‘/UI/interact_text’)), I get an error saying the node is not found. The issue seems to be that the current_scene is still pointing to the main menu during the transition, even though the new scene has been added. How can I correctly switch to the new scene and access its nodes without errors?
This is my code for the main_screen.gd,
extends Control
@export var bg_music: AudioStreamPlayer
var current_map_scene: Node = null
func _ready():
bg_music.play()
Load and switch to Map A
func level1():
switch_to_scene(“res://map_a/scene/main_menu.tscn”)
Load and switch to Map B
func level2():
switch_to_scene(“res://map_b/Walls/TestLevel/TestLevel.tscn”)
func switch_to_scene(scene_path: String):
if current_map_scene and current_map_scene in get_tree().root.get_children():
current_map_scene.queue_free()
current_map_scene = load(scene_path).instantiate()
get_tree().root.call_deferred("add_child", current_map_scene)
bg_music.stop()
func _on_quit_button_pressed():
get_tree().quit()