How to change scene in Godot 4?

Godot Version

v4.2.1.stable.flathub [b09f793f5]

Question

Brand new Godot project, I have two scenes main.tscn and menus/login.tscn.
main.tscn is the main scene, has a Control node as root and the following script attached.

extends Control


func _ready() -> void:
	# TODO: If not GameLauncher provided user ID, or locally stored ID.
	if true:
		get_tree().change_scene_to_file("res://menus/login.tscn")

When I run it changes the scene but logs the following error:

E 0:00:00:0421   main.gd:8 @ _ready(): Parent node is busy adding/removing children, `remove_child()` can't be called at this time. Consider using `remove_child.call_deferred(child)` instead.
  <C++ Error>    Condition "data.blocked > 0" is true.
  <C++ Source>   scene/main/node.cpp:1436 @ remove_child()
  <Stack Trace>  main.gd:8 @ _ready()

I found change_scene_to_file in the docs.

How am I supposed to change a scene in Godot 4?

Okay, I just figured it out, what I wanted is:

get_tree().call_deferred("change_scene_to_file", "res://menus/login.tscn")

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.