Class SceneTree
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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, retrieved 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 assigned 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 group's members at once.
SceneTree is the default MainLoop implementation used by scenes, and is thus in charge of the game loop.
public class SceneTree : MainLoop, IDisposable
- Inheritance
-
SceneTree
- Implements
- Inherited Members
Constructors
SceneTree()
public SceneTree()
Properties
AutoAcceptQuit
If true
, the application automatically accepts quitting requests.
For mobile platforms, see QuitOnGoBack.
public bool AutoAcceptQuit { get; set; }
Property Value
CurrentScene
Returns the root node of the currently running scene, regardless of its structure.
Warning: Setting this directly might not work as expected, and will not add or remove any nodes from the tree, consider using ChangeSceneToFile(string) or ChangeSceneToPacked(PackedScene) instead.
public Node CurrentScene { get; set; }
Property Value
DebugCollisionsHint
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 DebugCollisionsHint while the project is running will not have the desired effect.
public bool DebugCollisionsHint { get; set; }
Property Value
DebugNavigationHint
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 DebugNavigationHint while the project is running will not have the desired effect.
public bool DebugNavigationHint { get; set; }
Property Value
DebugPathsHint
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 DebugPathsHint while the project is running will not have the desired effect.
public bool DebugPathsHint { get; set; }
Property Value
EditedSceneRoot
The root of the edited scene.
public Node EditedSceneRoot { get; set; }
Property Value
MultiplayerPoll
If true
(default value), enables automatic polling of the MultiplayerApi for this SceneTree during ProcessFrame.
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.
public bool MultiplayerPoll { get; set; }
Property Value
Paused
If true
, the SceneTree is paused. Doing so will have the following behavior:
- 2D and 3D physics will be stopped. This includes signals and collision detection.
- _Process(double), _PhysicsProcess(double) and _Input(InputEvent) will not be called anymore in nodes.
public bool Paused { get; set; }
Property Value
QuitOnGoBack
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 GoBackRequest.
public bool QuitOnGoBack { get; set; }
Property Value
Root
public Window Root { get; }
Property Value
Methods
CallGroup(StringName, StringName, params Variant[])
Calls method
on each member of the given group. You can pass arguments to method
by specifying them at the end of the method call. If a node doesn't have the given method or the argument list does not match (either in count or in types), it will be skipped.
Note: CallGroup(StringName, StringName, params Variant[]) will call methods immediately on all members at once, which can cause stuttering if an expensive method is called on lots of members.
public void CallGroup(StringName group, StringName method, params Variant[] args)
Parameters
group
StringNamemethod
StringNameargs
Variant[]
CallGroupFlags(long, StringName, StringName, params Variant[])
Calls method
on each member of the given group, respecting the given SceneTree.GroupCallFlags. You can pass arguments to method
by specifying them at the end of the method call. If a node doesn't have the given method or the argument list does not match (either in count or in types), it will be skipped.
# Call the method in a deferred manner and in reverse order.
get_tree().call_group_flags(SceneTree.GROUP_CALL_DEFERRED | SceneTree.GROUP_CALL_REVERSE)
Note: Group call flags are used to control the method calling behavior. By default, methods will be called immediately in a way similar to CallGroup(StringName, StringName, params Variant[]). However, if the Deferred flag is present in the flags
argument, methods will be called at the end of the frame in a way similar to SetDeferred(StringName, Variant).
public void CallGroupFlags(long flags, StringName group, StringName method, params Variant[] args)
Parameters
flags
longgroup
StringNamemethod
StringNameargs
Variant[]
ChangeSceneToFile(string)
Changes the running scene to the one at the given path
, after loading it into a PackedScene and creating a new instance.
Returns Ok on success, CantOpen if the path
cannot be loaded into a PackedScene, or CantCreate if that scene cannot be instantiated.
Note: See ChangeSceneToPacked(PackedScene) for details on the order of operations.
public Error ChangeSceneToFile(string path)
Parameters
path
string
Returns
ChangeSceneToPacked(PackedScene)
Changes the running scene to a new instance of the given PackedScene (which must be valid).
Returns Ok on success, CantCreate if the scene cannot be instantiated, or InvalidParameter if the scene is invalid.
Note: Operations happen in the following order when ChangeSceneToPacked(PackedScene) is called:
1. The current scene node is immediately removed from the tree. From that point, GetTree() called on the current (outgoing) scene will return null
. CurrentScene 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. GetTree() and CurrentScene 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 QueueFree().
public Error ChangeSceneToPacked(PackedScene packedScene)
Parameters
packedScene
PackedScene
Returns
CreateTimer(double, bool, bool, bool)
Returns a SceneTreeTimer which will emit Timeout after the given time in seconds elapsed in this SceneTree.
If processAlways
is set to false
, pausing the SceneTree will also pause the timer.
If processInPhysics
is set to true
, will update the SceneTreeTimer during the physics frame instead of the process frame (fixed framerate processing).
If ignoreTimeScale
is set to true
, will ignore TimeScale and update the SceneTreeTimer with the actual frame delta.
Commonly used to create a one-shot delay timer as in the following example:
public async Task SomeFunction()
{
GD.Print("start");
await ToSignal(GetTree().CreateTimer(1.0f), SceneTreeTimer.SignalName.Timeout);
GD.Print("end");
}
The timer will be automatically freed after its time elapses.
Note: The timer is processed after all of the nodes in the current frame, i.e. node's _Process(double) method would be called before the timer (or _PhysicsProcess(double) if processInPhysics
is set to true
).
public SceneTreeTimer CreateTimer(double timeSec, bool processAlways = true, bool processInPhysics = false, bool ignoreTimeScale = false)
Parameters
Returns
CreateTween()
Creates and returns a new Tween. The Tween will start automatically on the next process frame or physics frame (depending on Tween.TweenProcessMode).
Note: When creating a Tween using this method, the Tween will not be tied to the Node that called it. It will continue to animate even if the Node is freed, but it will automatically finish if there's nothing left to animate. If you want the Tween to be automatically killed when the Node is freed, use CreateTween() or BindNode(Node).
public Tween CreateTween()
Returns
GetFirstNodeInGroup(StringName)
Returns the first node in the specified group, or null
if the group is empty or does not exist.
public Node GetFirstNodeInGroup(StringName group)
Parameters
group
StringName
Returns
GetFrame()
Returns the current frame number, i.e. the total frame count since the application started.
public long GetFrame()
Returns
GetMultiplayer(NodePath)
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 SetMultiplayer(MultiplayerApi, NodePath).
public MultiplayerApi GetMultiplayer(NodePath forPath = null)
Parameters
forPath
NodePath
Returns
GetNodeCount()
Returns the number of nodes in this SceneTree.
public int GetNodeCount()
Returns
GetNodesInGroup(StringName)
Returns a list of all nodes assigned to the given group.
public Array<Node> GetNodesInGroup(StringName group)
Parameters
group
StringName
Returns
GetProcessedTweens()
public Array<Tween> GetProcessedTweens()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
HasGroup(StringName)
Returns true
if the given group exists.
A group exists if any Node in the tree belongs to it (see AddToGroup(StringName, bool)). Groups without nodes are removed automatically.
public bool HasGroup(StringName name)
Parameters
name
StringName
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
NotifyGroup(StringName, int)
Sends the given notification to all members of the group
.
Note: NotifyGroup(StringName, int) will immediately notify all members at once, which can cause stuttering if an expensive method is called as a result of sending the notification to lots of members.
public void NotifyGroup(StringName group, int notification)
Parameters
group
StringNamenotification
int
NotifyGroupFlags(uint, StringName, int)
Sends the given notification to all members of the group
, respecting the given SceneTree.GroupCallFlags.
Note: Group call flags are used to control the notification sending behavior. By default, notifications will be sent immediately in a way similar to NotifyGroup(StringName, int). However, if the Deferred flag is present in the callFlags
argument, notifications will be sent at the end of the current frame in a way similar to using Object.call_deferred("notification", ...)
.
public void NotifyGroupFlags(uint callFlags, StringName group, int notification)
Parameters
callFlags
uintgroup
StringNamenotification
int
QueueDelete(GodotObject)
Queues the given object for deletion, delaying the call to Free() to the end of the current frame.
public void QueueDelete(GodotObject obj)
Parameters
obj
GodotObject
Quit(int)
Quits the application at the end of the current iteration. Argument exitCode
can optionally be given (defaulting to 0) to customize the exit status code.
By convention, an exit code of 0
indicates success whereas a non-zero exit code indicates an error.
For portability reasons, the exit code should be set 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.
public void Quit(int exitCode = 0)
Parameters
exitCode
int
ReloadCurrentScene()
Reloads the currently active scene.
Returns Ok on success, Unconfigured if no CurrentScene was defined yet, CantOpen if CurrentScene cannot be loaded into a PackedScene, or CantCreate if the scene cannot be instantiated.
public Error ReloadCurrentScene()
Returns
SetGroup(StringName, string, Variant)
Sets the given property
to value
on all members of the given group.
Note: SetGroup(StringName, string, Variant) will set the property immediately on all members at once, which can cause stuttering if a property with an expensive setter is set on lots of members.
public void SetGroup(StringName group, string property, Variant value)
Parameters
group
StringNameproperty
stringvalue
Variant
SetGroupFlags(uint, StringName, string, Variant)
Sets the given property
to value
on all members of the given group, respecting the given SceneTree.GroupCallFlags.
Note: Group call flags are used to control the property setting behavior. By default, properties will be set immediately in a way similar to SetGroup(StringName, string, Variant). However, if the Deferred flag is present in the callFlags
argument, properties will be set at the end of the frame in a way similar to CallDeferred(StringName, params Variant[]).
public void SetGroupFlags(uint callFlags, StringName group, string property, Variant value)
Parameters
callFlags
uintgroup
StringNameproperty
stringvalue
Variant
SetMultiplayer(MultiplayerApi, NodePath)
Sets a custom MultiplayerApi with the given rootPath
(controlling also the relative subpaths), or override the default one if rootPath
is empty.
Note: No MultiplayerApi must be configured for the subpath containing rootPath
, 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.
public void SetMultiplayer(MultiplayerApi multiplayer, NodePath rootPath = null)
Parameters
multiplayer
MultiplayerApirootPath
NodePath
UnloadCurrentScene()
If a current scene is loaded, calling this method will unload it.
public void UnloadCurrentScene()
Events
NodeAdded
Emitted whenever a node is added to the SceneTree.
public event SceneTree.NodeAddedEventHandler NodeAdded
Event Type
NodeConfigurationWarningChanged
Emitted when a node's configuration changed. Only emitted in tool
mode.
public event SceneTree.NodeConfigurationWarningChangedEventHandler NodeConfigurationWarningChanged
Event Type
NodeRemoved
Emitted whenever a node is removed from the SceneTree.
public event SceneTree.NodeRemovedEventHandler NodeRemoved
Event Type
NodeRenamed
Emitted whenever a node is renamed.
public event SceneTree.NodeRenamedEventHandler NodeRenamed
Event Type
PhysicsFrame
Emitted immediately before _PhysicsProcess(double) is called on every node in the SceneTree.
public event Action PhysicsFrame
Event Type
ProcessFrame
Emitted immediately before _Process(double) is called on every node in the SceneTree.
public event Action ProcessFrame
Event Type
TreeChanged
Emitted whenever the SceneTree hierarchy changed (children being moved or renamed, etc.).
public event Action TreeChanged
Event Type
TreeProcessModeChanged
This signal is only emitted in the editor, it allows the editor to update the visibility of disabled nodes. Emitted whenever any node's ProcessMode is changed.
public event Action TreeProcessModeChanged