Table of Contents

Class SceneTree

Manages the game loop via a hierarchy of nodes.

Inheritance
SceneTree

Remarks

As one of the most important classes, the SceneTree manages the hierarchy of nodes in a scene, as well as scenes themselves. Nodes can be added, fetched and removed. The whole scene tree (and thus the current scene) can be paused. Scenes can be loaded, switched and reloaded.

You can also use the SceneTree to organize your nodes into groups: every node can be added to as many groups as you want to create, e.g. an "enemy" group. You can then iterate these groups or even call methods and set properties on all the nodes belonging to any given group.

SceneTree is the default MainLoop implementation used by the engine, and is thus in charge of the game loop.

See Also

Properties

auto_accept_quit

If true, the application automatically accepts quitting requests.

For mobile platforms, see quit_on_go_back.

var auto_accept_quit : bool = true

Property Value

bool

Remarks

  • void set_auto_accept_quit(bool value)
  • bool is_auto_accept_quit

current_scene

The root node of the currently loaded main scene, usually as a direct child of root. See also SceneTree.change_scene_to_file, SceneTree.change_scene_to_packed, and reload_current_scene.

Warning: Setting this property directly may not work as expected, as it does not add or remove any nodes from this tree.

var current_scene : Node

Property Value

Node

Remarks

  • void set_current_scene(Node value)
  • Node get_current_scene

debug_collisions_hint

If true, collision shapes will be visible when running the game from the editor for debugging purposes.

Note: This property is not designed to be changed at run-time. Changing the value of debug_collisions_hint while the project is running will not have the desired effect.

var debug_collisions_hint : bool = false

Property Value

bool

Remarks

  • void set_debug_collisions_hint(bool value)
  • bool is_debugging_collisions_hint

debug_navigation_hint

If true, navigation polygons will be visible when running the game from the editor for debugging purposes.

Note: This property is not designed to be changed at run-time. Changing the value of debug_navigation_hint while the project is running will not have the desired effect.

var debug_navigation_hint : bool = false

Property Value

bool

Remarks

  • void set_debug_navigation_hint(bool value)
  • bool is_debugging_navigation_hint

debug_paths_hint

If true, curves from Path2D and Path3D nodes will be visible when running the game from the editor for debugging purposes.

Note: This property is not designed to be changed at run-time. Changing the value of debug_paths_hint while the project is running will not have the desired effect.

var debug_paths_hint : bool = false

Property Value

bool

Remarks

  • void set_debug_paths_hint(bool value)
  • bool is_debugging_paths_hint

edited_scene_root

The root of the scene currently being edited in the editor. This is usually a direct child of root.

Note: This property does nothing in release builds.

var edited_scene_root : Node

Property Value

Node

Remarks

  • void set_edited_scene_root(Node value)
  • Node get_edited_scene_root

multiplayer_poll

If true (default value), enables automatic polling of the MultiplayerAPI for this SceneTree during process_frame.

If false, you need to manually call poll to process network packets and deliver RPCs. This allows running RPCs in a different loop (e.g. physics, thread, specific time step) and for manual Mutex protection when accessing the MultiplayerAPI from threads.

var multiplayer_poll : bool = true

Property Value

bool

Remarks

  • void set_multiplayer_poll_enabled(bool value)
  • bool is_multiplayer_poll_enabled

paused

If true, the scene tree is considered paused. This causes the following behavior:

  • 2D and 3D physics will be stopped, as well as collision detection and related signals.

  • Depending on each node's process_mode, their Node._process, Node._physics_process and Node._input callback methods may not called anymore.

var paused : bool = false

Property Value

bool

Remarks

  • void set_pause(bool value)
  • bool is_paused

physics_interpolation

If true, the renderer will interpolate the transforms of physics objects between the last two transforms, so that smooth motion is seen even when physics ticks do not coincide with rendered frames.

The default value of this property is controlled by physics/common/physics_interpolation.

var physics_interpolation : bool = false

Property Value

bool

Remarks

  • void set_physics_interpolation_enabled(bool value)
  • bool is_physics_interpolation_enabled

quit_on_go_back

If true, the application quits automatically when navigating back (e.g. using the system "Back" button on Android).

To handle 'Go Back' button when this option is disabled, use DisplayServer.WINDOW_EVENT_GO_BACK_REQUEST.

var quit_on_go_back : bool = true

Property Value

bool

Remarks

  • void set_quit_on_go_back(bool value)
  • bool is_quit_on_go_back

root

The tree's root Window. This is top-most Node of the scene tree, and is always present. An absolute NodePath always starts from this node. Children of the root node may include the loaded current_scene, as well as any AutoLoad configured in the Project Settings.

Warning: Do not delete this node. This will result in unstable behavior, followed by a crash.

var root : Window

Property Value

Window

Remarks

Methods

call_group(StringName, StringName, ...)

Qualifiers: vararg

Calls method on each node inside this tree added to the given group. You can pass arguments to method by specifying them at the end of this method call. Nodes that cannot call method (either because the method doesn't exist or the arguments do not match) are ignored. See also SceneTree.set_group and SceneTree.notify_group.

Note: This method acts immediately on all selected nodes at once, which may cause stuttering in some performance-intensive situations.

Note: In C#, method must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the MethodName class to avoid allocating a new StringName on each call.

void call_group(StringName group, StringName method, ...)

Parameters

group StringName
method StringName

call_group_flags(int, StringName, StringName, ...)

Qualifiers: vararg

Calls the given method on each node inside this tree added to the given group. Use flags to customize this method's behavior (see GroupCallFlags). Additional arguments for method can be passed at the end of this method. Nodes that cannot call method (either because the method doesn't exist or the arguments do not match) are ignored.

# Calls "hide" to all nodes of the "enemies" group, at the end of the frame and in reverse tree order.
get_tree().call_group_flags(
        SceneTree.GROUP_CALL_DEFERRED | SceneTree.GROUP_CALL_REVERSE,
        "enemies", "hide")

Note: In C#, method must be in snake_case when referring to built-in Godot methods. Prefer using the names exposed in the MethodName class to avoid allocating a new StringName on each call.

void call_group_flags(int flags, StringName group, StringName method, ...)

Parameters

flags int
group StringName
method StringName

change_scene_to_file(String)

Changes the running scene to the one at the given path, after loading it into a PackedScene and creating a new instance.

Returns @GlobalScope.OK on success, @GlobalScope.ERR_CANT_OPEN if the path cannot be loaded into a PackedScene, or @GlobalScope.ERR_CANT_CREATE if that scene cannot be instantiated.

Note: See SceneTree.change_scene_to_packed for details on the order of operations.

int change_scene_to_file(String path)

Parameters

path String

change_scene_to_packed(PackedScene)

Changes the running scene to a new instance of the given PackedScene (which must be valid).

Returns @GlobalScope.OK on success, @GlobalScope.ERR_CANT_CREATE if the scene cannot be instantiated, or @GlobalScope.ERR_INVALID_PARAMETER if the scene is invalid.

Note: Operations happen in the following order when SceneTree.change_scene_to_packed is called:

  1. The current scene node is immediately removed from the tree. From that point, get_tree called on the current (outgoing) scene will return null. current_scene will be null, too, because the new scene is not available yet.

  2. At the end of the frame, the formerly current scene, already removed from the tree, will be deleted (freed from memory) and then the new scene will be instantiated and added to the tree. get_tree and current_scene will be back to working as usual.

This ensures that both scenes aren't running at the same time, while still freeing the previous scene in a safe way similar to queue_free.

int change_scene_to_packed(PackedScene packed_scene)

Parameters

packed_scene PackedScene

create_timer(float, bool, bool, bool)

Returns a new SceneTreeTimer. After time_sec in seconds have passed, the timer will emit timeout and will be automatically freed.

If process_always is false, the timer will be paused when setting paused to true.

If process_in_physics is true, the timer will update at the end of the physics frame, instead of the process frame.

If ignore_time_scale is true, the timer will ignore time_scale and update with the real, elapsed time.

This method is commonly used to create a one-shot delay timer, as in the following example:

func some_function():
    print("start")
    await get_tree().create_timer(1.0).timeout
    print("end")

Note: The timer is always updated after all of the nodes in the tree. A node's Node._process method would be called before the timer updates (or Node._physics_process if process_in_physics is set to true).

SceneTreeTimer create_timer(float time_sec, bool process_always, bool process_in_physics, bool ignore_time_scale)

Parameters

time_sec float
process_always bool
process_in_physics bool
ignore_time_scale bool

create_tween

Creates and returns a new Tween processed in this tree. The Tween will start automatically on the next process frame or physics frame (depending on its TweenProcessMode).

Note: A Tween created using this method is not bound to any Node. It may keep working until there is nothing left to animate. If you want the Tween to be automatically killed when the Node is freed, use create_tween or Tween.bind_node.

Tween create_tween

get_first_node_in_group(StringName)

Returns the first Node found inside the tree, that has been added to the given group, in scene hierarchy order. Returns null if no match is found. See also SceneTree.get_nodes_in_group.

Node get_first_node_in_group(StringName group)

Parameters

group StringName

get_frame

Qualifiers: const

Returns how many frames have been processed, since the application started. This is not a measurement of elapsed time.

int get_frame

get_multiplayer(NodePath)

Qualifiers: const

Searches for the MultiplayerAPI configured for the given path, if one does not exist it searches the parent paths until one is found. If the path is empty, or none is found, the default one is returned. See SceneTree.set_multiplayer.

MultiplayerAPI get_multiplayer(NodePath for_path)

Parameters

for_path NodePath

get_node_count

Qualifiers: const

Returns the number of nodes inside this tree.

int get_node_count

get_node_count_in_group(StringName)

Qualifiers: const

Returns the number of nodes assigned to the given group.

int get_node_count_in_group(StringName group)

Parameters

group StringName

get_nodes_in_group(StringName)

Returns an Array containing all nodes inside this tree, that have been added to the given group, in scene hierarchy order.

Node[] get_nodes_in_group(StringName group)

Parameters

group StringName

get_processed_tweens

Returns an Array of currently existing Tweens in the tree, including paused tweens.

Tween[] get_processed_tweens

has_group(StringName)

Qualifiers: const

Returns true if a node added to the given group name exists in the tree.

bool has_group(StringName name)

Parameters

name StringName

notify_group(StringName, int)

Calls Object.notification with the given notification to all nodes inside this tree added to the group. See also Godot notifications and SceneTree.call_group and SceneTree.set_group.

Note: This method acts immediately on all selected nodes at once, which may cause stuttering in some performance-intensive situations.

void notify_group(StringName group, int notification)

Parameters

group StringName
notification int

notify_group_flags(int, StringName, int)

Calls Object.notification with the given notification to all nodes inside this tree added to the group. Use call_flags to customize this method's behavior (see GroupCallFlags).

void notify_group_flags(int call_flags, StringName group, int notification)

Parameters

call_flags int
group StringName
notification int

queue_delete(Object)

Queues the given obj to be deleted, calling its free at the end of the current frame. This method is similar to queue_free.

void queue_delete(Object obj)

Parameters

obj Object

quit(int)

Quits the application at the end of the current iteration, with the given exit_code.

By convention, an exit code of 0 indicates success, whereas any other exit code indicates an error. For portability reasons, it should be between 0 and 125 (inclusive).

Note: On iOS this method doesn't work. Instead, as recommended by the iOS Human Interface Guidelines, the user is expected to close apps via the Home button.

void quit(int exit_code)

Parameters

exit_code int

reload_current_scene

Reloads the currently active scene, replacing current_scene with a new instance of its original PackedScene.

Returns @GlobalScope.OK on success, @GlobalScope.ERR_UNCONFIGURED if no current_scene is defined, @GlobalScope.ERR_CANT_OPEN if current_scene cannot be loaded into a PackedScene, or @GlobalScope.ERR_CANT_CREATE if the scene cannot be instantiated.

int reload_current_scene

set_group(StringName, String, Variant)

Sets the given property to value on all nodes inside this tree added to the given group. Nodes that do not have the property are ignored. See also SceneTree.call_group and SceneTree.notify_group.

Note: This method acts immediately on all selected nodes at once, which may cause stuttering in some performance-intensive situations.

Note: In C#, property must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the PropertyName class to avoid allocating a new StringName on each call.

void set_group(StringName group, String property, Variant value)

Parameters

group StringName
property String
value Variant

set_group_flags(int, StringName, String, Variant)

Sets the given property to value on all nodes inside this tree added to the given group. Nodes that do not have the property are ignored. Use call_flags to customize this method's behavior (see GroupCallFlags).

Note: In C#, property must be in snake_case when referring to built-in Godot properties. Prefer using the names exposed in the PropertyName class to avoid allocating a new StringName on each call.

void set_group_flags(int call_flags, StringName group, String property, Variant value)

Parameters

call_flags int
group StringName
property String
value Variant

set_multiplayer(MultiplayerAPI, NodePath)

Sets a custom MultiplayerAPI with the given root_path (controlling also the relative subpaths), or override the default one if root_path is empty.

Note: No MultiplayerAPI must be configured for the subpath containing root_path, nested custom multiplayers are not allowed. I.e. if one is configured for "/root/Foo" setting one for "/root/Foo/Bar" will cause an error.

void set_multiplayer(MultiplayerAPI multiplayer, NodePath root_path)

Parameters

multiplayer MultiplayerAPI
root_path NodePath

unload_current_scene

If a current scene is loaded, calling this method will unload it.

void unload_current_scene

Events

node_added(Node)

Emitted when the node enters this tree.

signal node_added(Node node)

Parameters

node Node

node_configuration_warning_changed(Node)

Emitted when the node's update_configuration_warnings is called. Only emitted in the editor.

signal node_configuration_warning_changed(Node node)

Parameters

node Node

node_removed(Node)

Emitted when the node exits this tree.

signal node_removed(Node node)

Parameters

node Node

node_renamed(Node)

Emitted when the node's name is changed.

signal node_renamed(Node node)

Parameters

node Node

physics_frame

Emitted immediately before Node._physics_process is called on every node in this tree.

signal physics_frame

process_frame

Emitted immediately before Node._process is called on every node in this tree.

signal process_frame

tree_changed

Emitted any time the tree's hierarchy changes (nodes being moved, renamed, etc.).

signal tree_changed

tree_process_mode_changed

Emitted when the process_mode of any node inside the tree is changed. Only emitted in the editor, to update the visibility of disabled nodes.

signal tree_process_mode_changed