SceneTree

package
v0.0.0-...-357ca8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 31 Imported by: 1

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(node Node.Any)

Add the given node to the scene tree.

func AddNamed

func AddNamed(name string, node Node.Any)

AddNamed adds the given node to the scene tree with the given name.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsSceneTree() Instance
}

type Expanded

type Expanded = MoreArgs

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsMainLoop

func (self *Extension[T]) AsMainLoop() MainLoop.Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsSceneTree

func (self *Extension[T]) AsSceneTree() Instance

type GroupCallFlags

type GroupCallFlags int //gd:SceneTree.GroupCallFlags
const (
	// Call nodes within a group with no special behavior (default).
	GroupCallDefault GroupCallFlags = 0
	// Call nodes within a group in reverse tree hierarchy order (all nested children are called before their respective parent nodes).
	GroupCallReverse GroupCallFlags = 1
	// Call nodes within a group at the end of the current frame (can be either process or physics frame), similar to [Object.CallDeferred].
	//
	// [Object.CallDeferred]: https://pkg.go.dev/graphics.gd/variant/Object#CallDeferred
	GroupCallDeferred GroupCallFlags = 2
	// Call nodes within a group only once, even if the call is executed many times in the same frame. Must be combined with [GroupCallDeferred] to work.
	//
	// Note: Different arguments are not taken into account. Therefore, when the same call is executed with different arguments, only the first call will be performed.
	GroupCallUnique GroupCallFlags = 4
)

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.SceneTree

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func Get

func Get(peer Node.Instance) Instance

Returns the [SceneTree] that contains this node. If this node is not inside the tree, generates an error and returns [code]null[/code]. See also [method is_inside_tree].

func New

func New() Instance

func (Instance) AsMainLoop

func (self Instance) AsMainLoop() MainLoop.Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsSceneTree

func (self Instance) AsSceneTree() Instance

func (Instance) AutoAcceptQuit

func (self Instance) AutoAcceptQuit() bool

If true, the application automatically accepts quitting requests.

For mobile platforms, see QuitOnGoBack.

func (Instance) CallGroup

func (self Instance) CallGroup(group string, method string, args ...any)

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 SetGroup and NotifyGroup.

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 string on each call.

func (Instance) CallGroupFlags

func (self Instance) CallGroupFlags(flags GroupCallFlags, group string, method string, args ...any)

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.
SceneTree.Get(node).CallGroupFlags(
	SceneTree.GroupCallDeferred|SceneTree.GroupCallReverse,
	"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 string on each call.

func (Instance) ChangeSceneToFile

func (self Instance) ChangeSceneToFile(path string) error

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, [ErrCantOpen] if the 'path' cannot be loaded into a PackedScene, or [ErrCantCreate] if that scene cannot be instantiated.

Note: See ChangeSceneToPacked for details on the order of operations.

func (Instance) ChangeSceneToPacked

func (self Instance) ChangeSceneToPacked(packed_scene PackedScene.Instance) error

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

Returns [Ok] on success, [ErrCantCreate] if the scene cannot be instantiated, or [ErrInvalidParameter] if the scene is invalid.

Note: Operations happen in the following order when ChangeSceneToPacked is called:

1. The current scene node is immediately removed from the tree. From that point, Node.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. Node.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 Node.QueueFree.

If you want to reliably access the new scene, await the OnSceneChanged signal.

func (Instance) CreateTimer

func (self Instance) CreateTimer(time_sec Float.X) SceneTreeTimer.Instance

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

If 'process_always' is false, the timer will be paused when setting SceneTree.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 Engine.TimeScale and update with the real, elapsed time.

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

fmt.Println("start")
SceneTree.Get(node).CreateTimer(1.0).OnTimeout(func() {
	fmt.Println("end")
}, Signal.OneShot)

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.PhysicsProcess if 'process_in_physics' is set to true).

func (Instance) CreateTween

func (self Instance) CreateTween() Tween.Instance

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 [Tween.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 Node.CreateTween or Tween.BindNode.

func (Instance) CurrentScene

func (self Instance) CurrentScene() Node.Instance

The root node of the currently loaded main scene, usually as a direct child of Root. See also ChangeSceneToFile, ChangeSceneToPacked, and ReloadCurrentScene.

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

func (Instance) DebugCollisionsHint

func (self Instance) DebugCollisionsHint() bool

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.

func (Instance) DebugNavigationHint

func (self Instance) DebugNavigationHint() bool

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.

func (Instance) DebugPathsHint

func (self Instance) DebugPathsHint() bool

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.

func (Instance) EditedSceneRoot

func (self Instance) EditedSceneRoot() Node.Instance

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.

func (Instance) GetFirstNodeInGroup

func (self Instance) GetFirstNodeInGroup(group string) Node.Instance

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 GetNodesInGroup.

func (Instance) GetFrame

func (self Instance) GetFrame() int

Returns how many physics process steps have been processed, since the application started. This is not a measurement of elapsed time. See also OnPhysicsFrame. For the number of frames rendered, see Engine.GetProcessFrames.

func (Instance) GetMultiplayer

func (self Instance) GetMultiplayer() MultiplayerAPI.Instance

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.

func (Instance) GetNodeCount

func (self Instance) GetNodeCount() int

Returns the number of nodes inside this tree.

func (Instance) GetNodeCountInGroup

func (self Instance) GetNodeCountInGroup(group string) int

Returns the number of nodes assigned to the given group.

func (Instance) GetNodesInGroup

func (self Instance) GetNodesInGroup(group string) []Node.Instance

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

func (Instance) GetProcessedTweens

func (self Instance) GetProcessedTweens() []Tween.Instance

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

func (Instance) HasGroup

func (self Instance) HasGroup(name string) bool

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

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsAccessibilityEnabled

func (self Instance) IsAccessibilityEnabled() bool

Returns true if accessibility features are enabled, and accessibility information updates are actively processed.

func (Instance) IsAccessibilitySupported

func (self Instance) IsAccessibilitySupported() bool

Returns true if accessibility features are supported by the OS and enabled in project settings.

func (Instance) MoreArgs

func (self Instance) MoreArgs() MoreArgs

MoreArgs enables certain functions to be called with additional 'optional' arguments.

func (Instance) MultiplayerPoll

func (self Instance) MultiplayerPoll() bool

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

If false, you need to manually call MultiplayerAPI.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.

func (Instance) NotifyGroup

func (self Instance) NotifyGroup(group string, notification int)

Calls Object.Notification with the given 'notification' to all nodes inside this tree added to the 'group'. See also Godot notifications and CallGroup and SetGroup.

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

func (Instance) NotifyGroupFlags

func (self Instance) NotifyGroupFlags(call_flags int, group string, notification 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).

func (Instance) OnNodeAdded

func (self Instance) OnNodeAdded(cb func(node Node.Instance), flags ...Signal.Flags)

Emitted when the 'node' enters this tree.

func (Instance) OnNodeConfigurationWarningChanged

func (self Instance) OnNodeConfigurationWarningChanged(cb func(node Node.Instance), flags ...Signal.Flags)

Emitted when the 'node”s Node.UpdateConfigurationWarnings is called. Only emitted in the editor.

func (Instance) OnNodeRemoved

func (self Instance) OnNodeRemoved(cb func(node Node.Instance), flags ...Signal.Flags)

Emitted when the 'node' exits this tree.

func (Instance) OnNodeRenamed

func (self Instance) OnNodeRenamed(cb func(node Node.Instance), flags ...Signal.Flags)

Emitted when the 'node”s Node.Name is changed.

func (Instance) OnPhysicsFrame

func (self Instance) OnPhysicsFrame(cb func(), flags ...Signal.Flags)

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

func (Instance) OnProcessFrame

func (self Instance) OnProcessFrame(cb func(), flags ...Signal.Flags)

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

func (Instance) OnSceneChanged

func (self Instance) OnSceneChanged(cb func(), flags ...Signal.Flags)

Emitted after the new scene is added to scene tree and initialized. Can be used to reliably access CurrentScene when changing scenes.

func (Instance) OnTreeChanged

func (self Instance) OnTreeChanged(cb func(), flags ...Signal.Flags)

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

func (Instance) OnTreeProcessModeChanged

func (self Instance) OnTreeProcessModeChanged(cb func(), flags ...Signal.Flags)

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

func (Instance) Paused

func (self Instance) Paused() bool

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 Node.ProcessMode, their Node.Process, Node.PhysicsProcess and Node.Input callback methods may not called anymore.

func (Instance) PhysicsInterpolation

func (self Instance) PhysicsInterpolation() bool

If true, the renderer will interpolate the transforms of objects (both physics and non-physics) 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 ProjectSettings "physics/common/physics_interpolation".

Note: Although this is a global setting, finer control of individual branches of the SceneTree is possible using Node.PhysicsInterpolationMode.

func (Instance) QueueDelete

func (self Instance) QueueDelete(obj Object.Instance)

Queues the given 'obj' to be deleted, calling its Object.Free at the end of the current frame. This method is similar to Node.QueueFree.

func (Instance) Quit

func (self Instance) Quit()

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.

func (Instance) QuitOnGoBack

func (self Instance) QuitOnGoBack() bool

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.WindowEventGoBackRequest].

func (Instance) ReloadCurrentScene

func (self Instance) ReloadCurrentScene() error

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

Returns [Ok] on success, [ErrUnconfigured] if no CurrentScene is defined, [ErrCantOpen] if CurrentScene cannot be loaded into a PackedScene, or [ErrCantCreate] if the scene cannot be instantiated.

func (Instance) Root

func (self Instance) Root() Window.Instance

The tree's root Window. This is top-most Node of the scene tree, and is always present. An absolute node path always starts from this node. Children of the root node may include the loaded CurrentScene, 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.

func (Instance) SetAutoAcceptQuit

func (self Instance) SetAutoAcceptQuit(value bool)

SetAutoAcceptQuit sets the property returned by [IsAutoAcceptQuit].

func (Instance) SetCurrentScene

func (self Instance) SetCurrentScene(value Node.Instance)

SetCurrentScene sets the property returned by [GetCurrentScene].

func (Instance) SetDebugCollisionsHint

func (self Instance) SetDebugCollisionsHint(value bool)

SetDebugCollisionsHint sets the property returned by [IsDebuggingCollisionsHint].

func (Instance) SetDebugNavigationHint

func (self Instance) SetDebugNavigationHint(value bool)

SetDebugNavigationHint sets the property returned by [IsDebuggingNavigationHint].

func (Instance) SetDebugPathsHint

func (self Instance) SetDebugPathsHint(value bool)

SetDebugPathsHint sets the property returned by [IsDebuggingPathsHint].

func (Instance) SetEditedSceneRoot

func (self Instance) SetEditedSceneRoot(value Node.Instance)

SetEditedSceneRoot sets the property returned by [GetEditedSceneRoot].

func (Instance) SetGroup

func (self Instance) SetGroup(group string, property string, value any)

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 CallGroup and NotifyGroup.

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 string on each call.

func (Instance) SetGroupFlags

func (self Instance) SetGroupFlags(call_flags int, group string, property string, value any)

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 string on each call.

func (Instance) SetMultiplayer

func (self Instance) SetMultiplayer(multiplayer MultiplayerAPI.Instance)

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.

Note: SetMultiplayer should be called before the child nodes are ready at the given 'root_path'. If multiplayer nodes like MultiplayerSpawner or MultiplayerSynchronizer are added to the tree before the custom multiplayer API is set, they will not work.

func (Instance) SetMultiplayerPoll

func (self Instance) SetMultiplayerPoll(value bool)

SetMultiplayerPoll sets the property returned by [IsMultiplayerPollEnabled].

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetPaused

func (self Instance) SetPaused(value bool)

SetPaused sets the property returned by [IsPaused].

func (Instance) SetPhysicsInterpolation

func (self Instance) SetPhysicsInterpolation(value bool)

SetPhysicsInterpolation sets the property returned by [IsPhysicsInterpolationEnabled].

func (Instance) SetQuitOnGoBack

func (self Instance) SetQuitOnGoBack(value bool)

SetQuitOnGoBack sets the property returned by [IsQuitOnGoBack].

func (Instance) UnloadCurrentScene

func (self Instance) UnloadCurrentScene()

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

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

type MoreArgs

type MoreArgs [1]gdclass.SceneTree

MoreArgs is a container for Instance functions with additional 'optional' arguments.

func (MoreArgs) CreateTimer

func (self MoreArgs) CreateTimer(time_sec Float.X, process_always bool, process_in_physics bool, ignore_time_scale bool) SceneTreeTimer.Instance

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

If 'process_always' is false, the timer will be paused when setting SceneTree.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 Engine.TimeScale and update with the real, elapsed time.

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

fmt.Println("start")
SceneTree.Get(node).CreateTimer(1.0).OnTimeout(func() {
	fmt.Println("end")
}, Signal.OneShot)

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.PhysicsProcess if 'process_in_physics' is set to true).

func (MoreArgs) GetMultiplayer

func (self MoreArgs) GetMultiplayer(for_path string) MultiplayerAPI.Instance

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.

func (MoreArgs) Quit

func (self MoreArgs) Quit(exit_code 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.

func (MoreArgs) SetMultiplayer

func (self MoreArgs) SetMultiplayer(multiplayer MultiplayerAPI.Instance, root_path string)

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.

Note: SetMultiplayer should be called before the child nodes are ready at the given 'root_path'. If multiplayer nodes like MultiplayerSpawner or MultiplayerSynchronizer are added to the tree before the custom multiplayer API is set, they will not work.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL