Node
Inherits: Object
Inherited By: AnimationPlayer, AnimationTree, AnimationTreePlayer, AudioStreamPlayer, CanvasItem, CanvasLayer, EditorFileSystem, EditorInterface, EditorPlugin, EditorResourcePreview, HTTPRequest, InstancePlaceholder, ResourcePreloader, SkeletonIK, Spatial, Timer, Tween, Viewport, WorldEnvironment
Category: Core
Brief Description
Base class for all scene objects.
Properties
MultiplayerAPI | custom_multiplayer |
String | filename |
MultiplayerAPI | multiplayer |
String | name |
Node | owner |
PauseMode | pause_mode |
Methods
Signals
- ready ( )
Emitted when the node is ready.
- renamed ( )
Emitted when the node is renamed.
- tree_entered ( )
Emitted when the node enters the tree.
- tree_exited ( )
Emitted after the node exits the tree and is no longer active.
- tree_exiting ( )
Emitted when the node is still active but about to exit the tree. This is the right place for de-initialization (or a “destructor”, if you will).
Enumerations
enum PauseMode:
- PAUSE_MODE_INHERIT = 0 — Inherits pause mode from the node’s parent. For the root node, it is equivalent to PAUSE_MODE_STOP. Default.
- PAUSE_MODE_STOP = 1 — Stop processing when the SceneTree is paused.
- PAUSE_MODE_PROCESS = 2 — Continue to process regardless of the SceneTree pause state.
enum DuplicateFlags:
- DUPLICATE_SIGNALS = 1 — Duplicate the node’s signals.
- DUPLICATE_GROUPS = 2 — Duplicate the node’s groups.
- DUPLICATE_SCRIPTS = 4 — Duplicate the node’s scripts.
- DUPLICATE_USE_INSTANCING = 8 — Duplicate using instancing.
Constants
- NOTIFICATION_ENTER_TREE = 10 — Notification received when the node enters a SceneTree.
- NOTIFICATION_EXIT_TREE = 11 — Notification received when the node is about to exit a SceneTree.
- NOTIFICATION_MOVED_IN_PARENT = 12 — Notification received when the node is moved in the parent.
- NOTIFICATION_READY = 13 — Notification received when the node is ready. See _ready.
- NOTIFICATION_PAUSED = 14 — Notification received when the node is paused.
- NOTIFICATION_UNPAUSED = 15 — Notification received when the node is unpaused.
- NOTIFICATION_PHYSICS_PROCESS = 16 — Notification received every frame when the physics process flag is set (see set_physics_process).
- NOTIFICATION_PROCESS = 17 — Notification received every frame when the process flag is set (see set_process).
- NOTIFICATION_PARENTED = 18 — Notification received when a node is set as a child of another node. Note that this doesn’t mean that a node entered the SceneTree.
- NOTIFICATION_UNPARENTED = 19 — Notification received when a node is unparented (parent removed it from the list of children).
- NOTIFICATION_INSTANCED = 20 — Notification received when the node is instanced.
- NOTIFICATION_DRAG_BEGIN = 21 — Notification received when a drag begins.
- NOTIFICATION_DRAG_END = 22 — Notification received when a drag ends.
- NOTIFICATION_PATH_CHANGED = 23 — Notification received when the node’s NodePath changed.
- NOTIFICATION_TRANSLATION_CHANGED = 24 — Notification received when translations may have changed. Can be triggered by the user changing the locale. Can be used to respond to language changes, for example to change the UI strings on the fly. Useful when working with the built-in translation support, like Object.tr.
- NOTIFICATION_INTERNAL_PROCESS = 25 — Notification received every frame when the internal process flag is set (see set_process_internal).
- NOTIFICATION_INTERNAL_PHYSICS_PROCESS = 26 — Notification received every frame when the internal physics process flag is set (see set_physics_process_internal).
Description
Nodes are Godot’s building blocks. They can be assigned as the child of another node, resulting in a tree arrangement. A given node can contain any number of nodes as children with the requirement that all siblings (direct children of a node) should have unique names.
A tree of nodes is called a scene. Scenes can be saved to the disk and then instanced into other scenes. This allows for very high flexibility in the architecture and data model of Godot projects.
Scene tree: The SceneTree contains the active tree of nodes. When a node is added to the scene tree, it receives the NOTIFICATION_ENTER_TREE notification and its _enter_tree callback is triggered. Child nodes are always added after their parent node, i.e. the _enter_tree callback of a parent node will be triggered before its child’s.
Once all nodes have been added in the scene tree, they receive the NOTIFICATION_READY notification and their respective _ready callbacks are triggered. For groups of nodes, the _ready callback is called in reverse order, starting with the children and moving up to the parent nodes.
This means that when adding a node to the scene tree, the following order will be used for the callbacks: _enter_tree of the parent, _enter_tree of the children, _ready of the children and finally _ready of the parent (recursively for the entire scene tree).
Processing: Nodes can override the “process” state, so that they receive a callback on each frame requesting them to process (do something). Normal processing (callback _process, toggled with set_process) happens as fast as possible and is dependent on the frame rate, so the processing time delta is passed as an argument. Physics processing (callback _physics_process, toggled with set_physics_process) happens a fixed number of times per second (60 by default) and is useful for code related to the physics engine.
Nodes can also process input events. When present, the _input function will be called for each input that the program receives. In many cases, this can be overkill (unless used for simple projects), and the _unhandled_input function might be preferred; it is called when the input event was not handled by anyone else (typically, GUI Control nodes), ensuring that the node only receives the events that were meant for it.
To keep track of the scene hierarchy (especially when instancing scenes into other scenes), an “owner” can be set for the node with the owner property. This keeps track of who instanced what. This is mostly useful when writing editors and tools, though.
Finally, when a node is freed with Object.free or queue_free, it will also free all its children.
Groups: Nodes can be added to as many groups as you want to be easy to manage, you could create groups like “enemies” or “collectables” for example, depending on your game. See add_to_group, is_in_group and remove_from_group. You can then retrieve all nodes in these groups, iterate them and even call methods on groups via the methods on SceneTree.
Networking with nodes: After connecting to a server (or making one, see NetworkedMultiplayerENet) it is possible to use the built-in RPC (remote procedure call) system to communicate over the network. By calling rpc with a method name, it will be called locally and in all connected peers (peers = clients and the server that accepts connections). To identify which node receives the RPC call Godot will use its NodePath (make sure node names are the same on all peers). Also take a look at the high-level networking tutorial and corresponding demos.
Tutorials
Property Descriptions
- MultiplayerAPI custom_multiplayer
Setter | set_custom_multiplayer(value) |
Getter | get_custom_multiplayer() |
The override to the default MultiplayerAPI. Set to null to use the default SceneTree one.
- String filename
Setter | set_filename(value) |
Getter | get_filename() |
When a scene is instanced from a file, its topmost node contains the filename from which it was loaded.
- MultiplayerAPI multiplayer
Getter | get_multiplayer() |
The MultiplayerAPI instance associated with this node. Either the custom_multiplayer, or the default SceneTree one (if inside tree).
- String name
Setter | set_name(value) |
Getter | get_name() |
The name of the node. This name is unique among the siblings (other child nodes from the same parent). When set to an existing name, the node will be automatically renamed
- Node owner
Setter | set_owner(value) |
Getter | get_owner() |
The node owner. A node can have any other node as owner (as long as it is a valid parent, grandparent, etc. ascending in the tree). When saving a node (using PackedScene) all the nodes it owns will be saved with it. This allows for the creation of complex SceneTrees, with instancing and subinstancing.
- PauseMode pause_mode
Setter | set_pause_mode(value) |
Getter | get_pause_mode() |
Pause mode. How the node will behave if the SceneTree is paused.
Method Descriptions
- void _enter_tree ( ) virtual
Called when the node enters the SceneTree (e.g. upon instancing, scene changing, or after calling add_child in a script). If the node has children, its _enter_tree callback will be called first, and then that of the children.
Corresponds to the NOTIFICATION_ENTER_TREE notification in Object._notification.
- void _exit_tree ( ) virtual
Called when the node is about to leave the SceneTree (e.g. upon freeing, scene changing, or after calling remove_child in a script). If the node has children, its _exit_tree callback will be called last, after all its children have left the tree.
Corresponds to the NOTIFICATION_EXIT_TREE notification in Object._notification and signal tree_exiting. To get notified when the node has already left the active tree, connect to the tree_exited
- String _get_configuration_warning ( ) virtual
The string returned from this method is displayed as a warning in the “Scene Dock” if the script that overrides it is a tool
script.
Returning an empty string produces no warning.
- void _input ( InputEvent event ) virtual
Called when there is an input event. The input event propagates up through the node tree until a node consumes it.
It is only called if input processing is enabled, which is done automatically if this method is overridden, and can be toggled with set_process_input.
To consume the input event and stop it propagating further to other nodes, SceneTree.set_input_as_handled can be called.
For gameplay input, _unhandled_input and _unhandled_key_input are usually a better fit as they allow the GUI to intercept the events first.
- void _physics_process ( float delta ) virtual
Called during the physics processing step of the main loop. Physics processing means that the frame rate is synced to the physics, i.e. the delta
variable should be constant.
It is only called if physics processing is enabled, which is done automatically if this method is overridden, and can be toggled with set_physics_process.
Corresponds to the NOTIFICATION_PHYSICS_PROCESS notification in Object._notification.
- void _process ( float delta ) virtual
Called during the processing step of the main loop. Processing happens at every frame and as fast as possible, so the delta
time since the previous frame is not constant.
It is only called if processing is enabled, which is done automatically if this method is overridden, and can be toggled with set_process.
Corresponds to the NOTIFICATION_PROCESS notification in Object._notification.
- void _ready ( ) virtual
Called when the node is “ready”, i.e. when both the node and its children have entered the scene tree. If the node has children, their _ready callbacks get triggered first, and the parent node will receive the ready notification afterwards.
Corresponds to the NOTIFICATION_READY notification in Object._notification. See also the onready
keyword for variables.
Usually used for initialization. For even earlier initialization, Object._init may be used. See also _enter_tree.
Note: _ready may be called only once for each node. After removing a node from the scene tree and adding again, _ready
will not be called for the second time. This can be bypassed with requesting another call with request_ready, which may be called anywhere before adding the node again.
- void _unhandled_input ( InputEvent event ) virtual
Called when an InputEvent hasn’t been consumed by _input or any GUI. The input event propagates up through the node tree until a node consumes it.
It is only called if unhandled input processing is enabled, which is done automatically if this method is overridden, and can be toggled with set_process_unhandled_input.
To consume the input event and stop it propagating further to other nodes, SceneTree.set_input_as_handled can be called.
For gameplay input, this and _unhandled_key_input are usually a better fit than _input as they allow the GUI to intercept the events first.
- void _unhandled_key_input ( InputEventKey event ) virtual
Called when an InputEventKey hasn’t been consumed by _input or any GUI. The input event propagates up through the node tree until a node consumes it.
It is only called if unhandled key input processing is enabled, which is done automatically if this method is overridden, and can be toggled with set_process_unhandled_key_input.
To consume the input event and stop it propagating further to other nodes, SceneTree.set_input_as_handled can be called.
For gameplay input, this and _unhandled_input are usually a better fit than _input as they allow the GUI to intercept the events first.
Adds a child node. Nodes can have any number of children, but every child must have a unique name. Child nodes are automatically deleted when the parent node is deleted, so an entire scene can be removed by deleting its topmost node.
Setting legible_unique_name
to true
creates child nodes with human-readable names, based on the name of the node being instanced instead of its type.
Adds a child node. The child is placed below the given node in the list of children.
Setting legible_unique_name
to true
creates child nodes with human-readable names, based on the name of the node being instanced instead of its type.
Adds the node to a group. Groups are helpers to name and organize a subset of nodes, for example “enemies” or “collectables”. A node can be in any number of groups. Nodes can be assigned a group at any time, but will not be added until they are inside the scene tree (see is_inside_tree). See notes in the description, and the group methods in SceneTree.
The persistent
option is used when packing node to PackedScene and saving to file. Non-persistent groups aren’t stored.
- bool can_process ( ) const
Returns true
if the node can process while the scene tree is paused (see pause_mode). Always returns true
if the scene tree is not paused, and false
if the node is not in the tree.
Duplicates the node, returning a new node.
You can fine-tune the behavior using the flags
(see DuplicateFlags).
Finds a descendant of this node whose name matches mask
as in String.match (i.e. case sensitive, but ‘*’ matches zero or more characters and ‘?’ matches any single character except ‘.’). Note that it does not match against the full path, just against individual node names.
If owned
is true
, this method only finds nodes whose owner is this node. This is especially important for scenes instantiated through script, because those scenes don’t have an owner.
Finds the first parent of the current node whose name matches mask
as in String.match (i.e. case sensitive, but ‘*’ matches zero or more characters and ‘?’ matches any single character except ‘.’). Note that it does not match against the full path, just against individual node names.
Returns a child node by its index (see get_child_count). This method is often used for iterating all children of a node.
To access a child node via its name, use get_node.
- int get_child_count ( ) const
Returns the number of child nodes.
- Array get_children ( ) const
Returns an array of references to node’s children.
- Array get_groups ( ) const
Returns an array listing the groups that the node is a member of.
- int get_index ( ) const
Returns the node’s index, i.e. its position among the siblings of its parent.
- int get_network_master ( ) const
Returns the peer ID of the network master for this node. See set_network_master.
Fetches a node. The NodePath can be either a relative path (from the current node) or an absolute path (in the scene tree) to a node. If the path does not exist, a null instance
is returned and attempts to access it will result in an “Attempt to call <method> on a null instance.” error.
Note: Fetching absolute paths only works when the node is inside the scene tree (see is_inside_tree).
Example: Assume your current node is Character and the following tree:
/root
/root/Character
/root/Character/Sword
/root/Character/Backpack/Dagger
/root/MyGame
/root/Swamp/Alligator
/root/Swamp/Mosquito
/root/Swamp/Goblin
Possible paths are:
get_node("Sword")
get_node("Backpack/Dagger")
get_node("../Swamp/Alligator")
get_node("/root/MyGame")
Similar to get_node, but does not raise an error when path
does not point to a valid Node
.
- Node get_parent ( ) const
Returns the parent node of the current node, or an empty Node
if the node lacks a parent.
- NodePath get_path ( ) const
Returns the absolute path of the current node. This only works if the current node is inside the scene tree (see is_inside_tree).
Returns the relative NodePath from this node to the specified node
. Both nodes must be in the same scene or the function will fail.
- float get_physics_process_delta_time ( ) const
Returns the time elapsed since the last physics-bound frame (see _physics_process). This is always a constant value in physics processing unless the frames per second is changed via Engine.target_fps.
- int get_position_in_parent ( ) const
Returns the node’s order in the scene tree branch. For example, if called on the first child node the position is 0
.
- float get_process_delta_time ( ) const
Returns the time elapsed (in seconds) since the last process callback. This value may vary from frame to frame.
- bool get_scene_instance_load_placeholder ( ) const
Returns true
if this is an instance load placeholder. See InstancePlaceholder.
- SceneTree get_tree ( ) const
Returns the SceneTree that contains this node.
- Viewport get_viewport ( ) const
Returns the node’s Viewport.
Returns true
if the node that the NodePath points to exists.
Returns true
if the given node is a direct or indirect child of the current node.
- bool is_displayed_folded ( ) const
Returns true
if the node is folded (collapsed) in the Scene dock.
Returns true
if the given node occurs later in the scene hierarchy than the current node.
Returns true
if this node is in the specified group. See notes in the description, and the group methods in SceneTree.
- bool is_inside_tree ( ) const
Returns true
if this node is currently inside a SceneTree.
- bool is_network_master ( ) const
Returns true
if the local system is the master of this node.
- bool is_physics_processing ( ) const
Returns true
if physics processing is enabled (see set_physics_process).
- bool is_physics_processing_internal ( ) const
Returns true
if internal physics processing is enabled (see set_physics_process_internal).
- bool is_processing ( ) const
Returns true
if processing is enabled (see set_process).
- bool is_processing_input ( ) const
Returns true
if the node is processing input (see set_process_input).
- bool is_processing_internal ( ) const
Returns true
if internal processing is enabled (see set_process_internal).
- bool is_processing_unhandled_input ( ) const
Returns true
if the node is processing unhandled input (see set_process_unhandled_input).
- bool is_processing_unhandled_key_input ( ) const
Returns true
if the node is processing unhandled key input (see set_process_unhandled_key_input).
Moves a child node to a different position (order) among the other children. Since calls, signals, etc are performed by tree order, changing the order of children nodes may be useful.
- void print_stray_nodes ( )
Prints all stray nodes (nodes outside the SceneTree). Used for debugging. Works only in debug builds.
- void print_tree ( )
Prints the tree to stdout. Used mainly for debugging purposes. This version displays the path relative to the current node, and is good for copy/pasting into the get_node function. Example output:
TheGame
TheGame/Menu
TheGame/Menu/Label
TheGame/Menu/Camera2D
TheGame/SplashScreen
TheGame/SplashScreen/Camera2D
- void print_tree_pretty ( )
Similar to print_tree, this prints the tree to stdout. This version displays a more graphical representation similar to what is displayed in the scene inspector. It is useful for inspecting larger trees. Example output:
┖╴TheGame
┠╴Menu
┃ ┠╴Label
┃ ┖╴Camera2D
┖-SplashScreen
┖╴Camera2D
Calls the given method (if present) with the arguments given in args
on this node and recursively on all its children. If the parent_first
argument is true
then the method will be called on the current node first, then on all children. If it is false
then the children will be called first.
- void propagate_notification ( int what )
Notifies the current node and all its children recursively by calling Object.notification on all of them.
- void queue_free ( )
Queues a node for deletion at the end of the current frame. When deleted, all of its child nodes will be deleted as well. This method ensures it’s safe to delete the node, contrary to Object.free. Use Object.is_queued_for_deletion to check whether a node will be deleted at the end of the frame.
- void raise ( )
Moves this node to the bottom of parent node’s children hierarchy. This is often useful in GUIs (Control nodes), because their order of drawing depends on their order in the tree, i.e. the further they are on the node list, the higher they are drawn. After using raise
, a Control will be drawn on top of their siblings.
- void remove_and_skip ( )
Removes a node and sets all its children as children of the parent node (if it exists). All event subscriptions that pass by the removed node will be unsubscribed.
- void remove_child ( Node node )
Removes a child node. The node is NOT deleted and must be deleted manually.
- void remove_from_group ( String group )
Removes a node from a group. See notes in the description, and the group methods in SceneTree.
Replaces a node in a scene by the given one. Subscriptions that pass through this node will be lost.
- void request_ready ( )
Requests that _ready
be called again. Note that the method won’t be called immediately, but is scheduled for when the node is added to the scene tree again (see _ready). _ready
is called only for the node which requested it, which means that you need to request ready for each child if you want them to call _ready
too (in which case, _ready
will be called in the same order as it would normally).
Sends a remote procedure call request for the given method
to peers on the network (and locally), optionally sending all additional arguments as arguments to the method called by the RPC. The call request will only be received by nodes with the same NodePath, including the exact same node name. Behaviour depends on the RPC configuration for the given method, see rpc_config. Methods are not exposed to RPCs by default. Also see rset and rset_config for properties. Returns an empty Variant. Note that you can only safely use RPCs on clients after you received the connected_to_server
signal from the SceneTree. You also need to keep track of the connection state, either by the SceneTree signals like server_disconnected
or by checking SceneTree.network_peer.get_connection_status() == CONNECTION_CONNECTED
.
Changes the RPC mode for the given method
to the given mode
. See RPCMode. An alternative is annotating methods and properties with the corresponding keywords (remote
, master
, puppet
, remotesync
, mastersync
, puppetsync
). By default, methods are not exposed to networking (and RPCs). Also see rset and rset_config for properties.
Sends a rpc to a specific peer identified by peer_id
(see NetworkedMultiplayerPeer.set_target_peer). Returns an empty Variant.
Sends a rpc using an unreliable protocol. Returns an empty Variant.
Sends a rpc to a specific peer identified by peer_id
using an unreliable protocol (see NetworkedMultiplayerPeer.set_target_peer). Returns an empty Variant.
Remotely changes a property’s value on other peers (and locally). Behaviour depends on the RPC configuration for the given property, see rset_config. Also see rpc for RPCs for methods, most information applies to this method as well.
Changes the RPC mode for the given property
to the given mode
. See RPCMode. An alternative is annotating methods and properties with the corresponding keywords (remote
, master
, puppet
, remotesync
, mastersync
, puppetsync
). By default, properties are not exposed to networking (and RPCs). Also see rpc and rpc_config for methods.
Remotely changes the property’s value on a specific peer identified by peer_id
(see NetworkedMultiplayerPeer.set_target_peer).
Remotely changes the property’s value on other peers (and locally) using an unreliable protocol.
Remotely changes property’s value on a specific peer identified by peer_id
using an unreliable protocol (see NetworkedMultiplayerPeer.set_target_peer).
- void set_display_folded ( bool fold )
Sets the folded state of the node in the Scene dock.
Sets the node’s network master to the peer with the given peer ID. The network master is the peer that has authority over the node on the network. Useful in conjunction with the master
and puppet
keywords. Inherited from the parent node by default, which ultimately defaults to peer ID 1 (the server). If recursive
, the given peer is recursively set as the master for all children of this node.
- void set_physics_process ( bool enable )
Enables or disables physics (i.e. fixed framerate) processing. When a node is being processed, it will receive a NOTIFICATION_PHYSICS_PROCESS at a fixed (usually 60 FPS, see Engine.target_fps to change) interval (and the _physics_process callback will be called if exists). Enabled automatically if _physics_process is overridden. Any calls to this before _ready will be ignored.
- void set_physics_process_internal ( bool enable )
Enables or disables internal physics for this node. Internal physics processing happens in isolation from the normal _physics_process calls and is used by some nodes internally to guarantee proper functioning even if the node is paused or physics processing is disabled for scripting (set_physics_process). Only useful for advanced uses to manipulate built-in nodes behavior.
- void set_process ( bool enable )
Enables or disables processing. When a node is being processed, it will receive a NOTIFICATION_PROCESS on every drawn frame (and the _process callback will be called if exists). Enabled automatically if _process is overridden. Any calls to this before _ready will be ignored.
- void set_process_input ( bool enable )
Enables or disables input processing. This is not required for GUI controls! Enabled automatically if _input is overridden. Any calls to this before _ready will be ignored.
- void set_process_internal ( bool enable )
Enables or disabled internal processing for this node. Internal processing happens in isolation from the normal _process calls and is used by some nodes internally to guarantee proper functioning even if the node is paused or processing is disabled for scripting (set_process). Only useful for advanced uses to manipulate built-in nodes behavior.
- void set_process_priority ( int priority )
Sets the node’s priority in the execution order of the enabled processing callbacks (i.e. NOTIFICATION_PROCESS, NOTIFICATION_PHYSICS_PROCESS and their internal counterparts). Nodes with a higher process priority will have their processing callbacks executed first.
- void set_process_unhandled_input ( bool enable )
Enables unhandled input processing. This is not required for GUI controls! It enables the node to receive all input that was not previously handled (usually by a Control). Enabled automatically if _unhandled_input is overridden. Any calls to this before _ready will be ignored.
- void set_process_unhandled_key_input ( bool enable )
Enables unhandled key input processing. Enabled automatically if _unhandled_key_input is overridden. Any calls to this before _ready will be ignored.
- void set_scene_instance_load_placeholder ( bool load_placeholder )
Sets whether this is an instance load placeholder. See InstancePlaceholder.