SceneTree Class |
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.
Namespace: Godot
public class SceneTree : MainLoop
The SceneTree type exposes the following members.
Name | Description | |
---|---|---|
CurrentScene | The current scene. | |
DebugCollisionsHint | If true, collision shapes will be visible when running the game from the editor for debugging purposes. | |
DebugNavigationHint | If true, navigation polygons will be visible when running the game from the editor for debugging purposes. | |
DynamicObject |
Gets a new DynamicGodotObject associated with this instance.
(Inherited from Object.) | |
EditedSceneRoot | The root of the edited scene. | |
Multiplayer | The default MultiplayerAPI instance for this SceneTree. | |
MultiplayerPoll | If true (default value), enables automatic polling of the MultiplayerAPI for this SceneTree during idle_frame. If false, you need to manually call Poll to process network packets and deliver RPCs/RSETs. This allows running RPCs/RSETs in a different loop (e.g. physics, thread, specific time step) and for manual Mutex protection when accessing the MultiplayerAPI from threads. | |
NativeInstance | (Inherited from Object.) | |
NetworkPeer | The peer object to handle the RPC system (effectively enabling networking when set). Depending on the peer itself, the SceneTree will become a network server (check with IsNetworkServer) and will set the root node's network mode to master, or it will become a regular peer with the root node set to puppet. All child nodes are set to inherit the network mode by default. Handling of networking-related events (connection, disconnection, new clients) is done by connecting to SceneTree's signals. | |
Paused | If true, the SceneTree is paused. Doing so will have the following behavior: - 2D and 3D physics will be stopped. - _Process(Single), _PhysicsProcess(Single) and _Input(InputEvent) will not be called anymore in nodes. | |
RefuseNewNetworkConnections | If true, the SceneTree's NetworkPeer refuses new incoming connections. | |
Root | The SceneTree's root Viewport. | |
UseFontOversampling | If true, font oversampling is used. |
Name | Description | |
---|---|---|
_DropFiles | Called when files are dragged from the OS file manager and dropped in the game window. The arguments are a list of file paths and the identifier of the screen where the drag originated. | |
_Finalize | Called before the program exits. | |
_Get | Virtual method which can be overridden to customize the return value of Get(String). Returns the given property. Returns null if the property does not exist. | |
_GetPropertyList | Virtual method which can be overridden to customize the return value of GetPropertyList. Returns the object's property list as an Array of dictionaries. Each property's Dictionary must contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags). | |
_GlobalMenuAction | Called when the user performs an action in the system global menu (e.g. the Mac OS menu bar). | |
_Idle | Called each idle frame with the time since the last idle frame as argument (in seconds). Equivalent to _Process(Single). If implemented, the method must return a boolean value. true ends the main loop, while false lets it proceed to the next frame. | |
_Initialize | Called once during initialization. | |
_InputEvent | Called whenever an InputEvent is received by the main loop. | |
_InputText | Deprecated callback, does not do anything. Use _InputEvent(InputEvent) to parse text input. Will be removed in Godot 4.0. | |
_Iteration | Called each physics frame with the time since the last physics frame as argument (delta, in seconds). Equivalent to _PhysicsProcess(Single). If implemented, the method must return a boolean value. true ends the main loop, while false lets it proceed to the next frame. | |
_Notification | Called whenever the object receives a notification, which is identified in what by a constant. The base Object has two constants and , but subclasses such as Node define a lot more notifications which are also received by this method. | |
_Set | Virtual method which can be overridden to customize the return value of Set(String, Object). Sets a property. Returns true if the property exists. | |
AddUserSignal | Adds a user-defined signal. Arguments are optional, but can be added as an Array of dictionaries, each containing name: String and type: int (see VariantType) entries. | |
Call | Calls the method on the object and returns the result. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: call("set", "position", Vector2(42.0, 0.0)) Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase). | |
CallDeferred | Calls the method on the object during idle time. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: call_deferred("set", "position", Vector2(42.0, 0.0)) Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase). | |
CallGroup | 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. Note: method may only have 5 arguments at most (7 arguments passed to this method in total). | |
CallGroupFlags | Calls method on each member of the given group, respecting the given SceneTreeGroupCallFlags. You can pass arguments to method by specifying them at the end of the method call. Note: method may only have 5 arguments at most (8 arguments passed to this method in total). | |
Callv | Calls the method on the object and returns the result. Contrarily to Call(String, Object), this method does not support a variable number of arguments but expects all parameters to be via a single Array. callv("set", [ "position", Vector2(42.0, 0.0) ]) | |
CanTranslateMessages | Returns true if the object can translate strings. See SetMessageTranslation(Boolean) and Tr(String). | |
ChangeScene | Changes the running scene to the one at the given path, after loading it into a PackedScene and creating a new instance. Returns on success, if the path cannot be loaded into a PackedScene, or if that scene cannot be instantiated. Note: The scene change is deferred, which means that the new scene node is added on the next idle frame. You won't be able to access it immediately after the ChangeScene(String) call. | |
ChangeSceneTo | Changes the running scene to a new instance of the given PackedScene. Returns on success or if the scene cannot be instantiated. Note: The scene change is deferred, which means that the new scene node is added on the next idle frame. You won't be able to access it immediately after the ChangeSceneTo(PackedScene) call. | |
Connect | Connects a signal to a method on a target object. Pass optional binds to the call as an Array of parameters. These parameters will be passed to the method after any parameter used in the call to EmitSignal(String, Object). Use flags to set deferred or one-shot connections. See ObjectConnectFlags constants. A signal can only be connected once to a method. It will throw an error if already connected, unless the signal was connected with . To avoid this, first, use IsConnected(String, Object, String) to check for existing connections. If the target is destroyed in the game's lifecycle, the connection will be lost. Examples: connect("pressed", self, "_on_Button_pressed") # BaseButton signal connect("text_entered", self, "_on_LineEdit_text_entered") # LineEdit signal connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # User-defined signal An example of the relationship between binds passed to Connect(String, Object, String, Array, UInt32) and parameters used when calling EmitSignal(String, Object): connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # weapon_type and damage are passed last emit_signal("hit", "Dark lord", 5) # "Dark lord" and 5 are passed first func _on_Player_hit(hit_by, level, weapon_type, damage): print("Hit by %s (lvl %d) with weapon %s for %d damage" % [hit_by, level, weapon_type, damage]) | |
CreateTimer | Returns a SceneTreeTimer which will SceneTreeTimer.timeout after the given time in seconds elapsed in this SceneTree. If pause_mode_process is set to false, pausing the SceneTree will also pause the timer. Commonly used to create a one-shot delay timer as in the following example: func some_function(): print("start") yield(get_tree().create_timer(1.0), "timeout") print("end") The timer will be automatically freed after its time elapses. | |
Disconnect | Disconnects a signal from a method on the given target. If you try to disconnect a connection that does not exist, the method will throw an error. Use IsConnected(String, Object, String) to ensure that the connection exists. | |
Dispose | (Inherited from Object.) | |
Dispose(Boolean) | (Inherited from Object.) | |
EmitSignal | Emits the given signal. The signal must exist, so it should be a built-in signal of this class or one of its parent classes, or a user-defined signal. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: emit_signal("hit", weapon_type, damage) emit_signal("game_over") | |
Equals | (Inherited from Object.) | |
Finalize | (Inherited from Object.) | |
Finish | Should not be called manually, override _Finalize instead. Will be removed in Godot 4.0. | |
Free | Deletes the object from memory immediately. For Nodes, you may want to use QueueFree to queue the node for safe deletion at the end of the current frame. Important: If you have a variable pointing to an object, it will not be assigned to null once the object is freed. Instead, it will point to a previously freed instance and you should validate it with @GDScript.is_instance_valid before attempting to call its methods or access its properties. | |
Get | Returns the Variant value of the given property. If the property doesn't exist, this will return null. Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
GetClass | Returns the object's class as a String. | |
GetCurrentScene | Obsolete. | |
GetEditedSceneRoot | Obsolete. | |
GetFrame | Returns the current frame number, i.e. the total frame count since the application started. | |
GetHashCode | (Inherited from Object.) | |
GetIncomingConnections | Returns an Array of dictionaries with information about signals that are connected to the object. Each Dictionary contains three String entries: - source is a reference to the signal emitter. - signal_name is the name of the connected signal. - method_name is the name of the method to which the signal is connected. | |
GetIndexed | Gets the object's property indexed by the given NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Examples: "position:x" or "material:next_pass:blend_mode". | |
GetInstanceId | Returns the object's unique instance ID. This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with @GDScript.instance_from_id. | |
GetMeta | Returns the object's metadata entry for the given name. | |
GetMetaList | Returns the object's metadata as a String. | |
GetMethodList | Returns the object's methods and their signatures as an Array. | |
GetMultiplayer | Obsolete. | |
GetNetworkConnectedPeers | Returns the peer IDs of all connected peers of this SceneTree's NetworkPeer. | |
GetNetworkPeer | Obsolete. | |
GetNetworkUniqueId | Returns the unique peer ID of this SceneTree's NetworkPeer. | |
GetNodeCount | Returns the number of nodes in this SceneTree. | |
GetNodesInGroup | Returns a list of all nodes assigned to the given group. | |
GetPropertyList | Returns the object's property list as an Array of dictionaries. Each property's Dictionary contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags). | |
GetRoot | Obsolete. | |
GetRpcSenderId | Returns the sender's peer ID for the most recently received RPC call. | |
GetScript | Returns the object's Script instance, or null if none is assigned. | |
GetSignalConnectionList | Returns an Array of connections for the given signal. | |
GetSignalList | Returns the list of signals as an Array of dictionaries. | |
GetType | (Inherited from Object.) | |
HasGroup | Returns true if the given group exists. | |
HasMeta | Returns true if a metadata entry is found with the given name. | |
HasMethod | Returns true if the object contains the given method. | |
HasNetworkPeer | Returns true if there is a NetworkPeer set. | |
HasSignal | Returns true if the given signal exists. | |
HasUserSignal | Returns true if the given user-defined signal exists. Only signals added using AddUserSignal(String, Array) are taken into account. | |
Idle | Should not be called manually, override _Idle(Single) instead. Will be removed in Godot 4.0. | |
Init | Should not be called manually, override _Initialize instead. Will be removed in Godot 4.0. | |
InputEvent | Should not be called manually, override _InputEvent(InputEvent) instead. Will be removed in Godot 4.0. | |
InputText | Should not be called manually, override _InputText(String) instead. Will be removed in Godot 4.0. | |
IsBlockingSignals | Returns true if signal emission blocking is enabled. | |
IsClass | Returns true if the object inherits from the given class. | |
IsConnected | Returns true if a connection exists for a given signal, target, and method. | |
IsDebuggingCollisionsHint | Obsolete. | |
IsDebuggingNavigationHint | Obsolete. | |
IsInputHandled | Returns true if the most recent InputEvent was marked as handled with SetInputAsHandled. | |
IsMultiplayerPollEnabled | Obsolete. | |
IsNetworkServer | Returns true if this SceneTree's NetworkPeer is in server mode (listening for connections). | |
IsPaused | Obsolete. | |
IsQueuedForDeletion | Returns true if the QueueFree method was called for the object. | |
IsRefusingNewNetworkConnections | Obsolete. | |
IsUsingFontOversampling | Obsolete. | |
Iteration | Should not be called manually, override _Iteration(Single) instead. Will be removed in Godot 4.0. | |
MemberwiseClone | (Inherited from Object.) | |
Notification | Send a given notification to the object, which will also trigger a call to the _Notification(Int32) method of all classes that the object inherits from. If reversed is true, _Notification(Int32) is called first on the object's own class, and then up to its successive parent classes. If reversed is false, _Notification(Int32) is called first on the highest ancestor (Object itself), and then down to its successive inheriting classes. | |
NotifyGroup | Sends the given notification to all members of the group. | |
NotifyGroupFlags | Sends the given notification to all members of the group, respecting the given SceneTreeGroupCallFlags. | |
PropertyListChangedNotify | Notify the editor that the property list has changed, so that editor plugins can take the new values into account. Does nothing on export builds. | |
QueueDelete | Queues the given object for deletion, delaying the call to Free to after the current frame. | |
Quit | Quits the application at the end of the current iteration. A process exit_code can optionally be passed as an argument. If this argument is 0 or greater, it will override the ExitCode defined before quitting the application. | |
ReloadCurrentScene | Reloads the currently active scene. Returns on success, if no CurrentScene was defined yet, if CurrentScene cannot be loaded into a PackedScene, or if the scene cannot be instantiated. | |
RemoveMeta | Removes a given entry from the object's metadata. See also SetMeta(String, Object). | |
Set | Assigns a new value to the given property. If the property does not exist, nothing will happen. Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
SetAutoAcceptQuit | If true, the application automatically accepts quitting. Enabled by default. For mobile platforms, see SetQuitOnGoBack(Boolean). | |
SetBlockSignals | If set to true, signal emission is blocked. | |
SetCurrentScene | Obsolete. | |
SetDebugCollisionsHint | Obsolete. | |
SetDebugNavigationHint | Obsolete. | |
SetDeferred | Assigns a new value to the given property, after the current frame's physics step. This is equivalent to calling Set(String, Object) via CallDeferred(String, Object), i.e. call_deferred("set", property, value). Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
SetEditedSceneRoot | Obsolete. | |
SetGroup | Sets the given property to value on all members of the given group. | |
SetGroupFlags | Sets the given property to value on all members of the given group, respecting the given SceneTreeGroupCallFlags. | |
SetIndexed | Assigns a new value to the property identified by the NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Example: set_indexed("position", Vector2(42, 0)) set_indexed("position:y", -10) print(position) # (42, -10) | |
SetInputAsHandled | Marks the most recent InputEvent as handled. | |
SetMessageTranslation | Defines whether the object can translate strings (with calls to Tr(String)). Enabled by default. | |
SetMeta | Adds, changes or removes a given entry in the object's metadata. Metadata are serialized and can take any Variant value. To remove a given entry from the object's metadata, use RemoveMeta(String). Metadata is also removed if its value is set to null. This means you can also use set_meta("name", null) to remove metadata for "name". | |
SetMultiplayer | Obsolete. | |
SetMultiplayerPollEnabled | Obsolete. | |
SetNetworkPeer | Obsolete. | |
SetPause | Obsolete. | |
SetQuitOnGoBack | If true, the application quits automatically on going back (e.g. on Android). Enabled by default. To handle 'Go Back' button when this option is disabled, use . | |
SetRefuseNewNetworkConnections | Obsolete. | |
SetScreenStretch | Configures screen stretching to the given SceneTreeStretchMode, SceneTreeStretchAspect, minimum size and shrink ratio. | |
SetScript | Assigns a script to the object. Each object can have a single script assigned to it, which are used to extend its functionality. If the object already had a script, the previous script instance will be freed and its variables and state will be lost. The new script's method will be called. | |
SetUseFontOversampling | Obsolete. | |
ToSignal |
Returns a new SignalAwaiter awaiter configured to complete when the instance
source emits the signal specified by the signal parameter.
(Inherited from Object.) | |
ToString | (Inherited from Object.) | |
Tr | Translates a message using translation catalogs configured in the Project Settings. Only works if message translation is enabled (which it is by default), otherwise it returns the message unchanged. See SetMessageTranslation(Boolean). |