Node creation and the ready function
_ready()
function for the first time. The _ready()
function is a function that Godot recognizes and calls in your scripts when a node and all its children are ready to access in code.- How Godot creates nodes when running the game.
- The order in which the engine adds nodes to the scene tree at runtime.
- How we usually use the
_ready()
function. - When Godot calls the
_ready()
function for each node, and in which order.
How Godot creates nodes when running the game
In which order Godot adds nodes to the scene tree
Am I limited to one window node?No, you're not! You can have as many viewports and window nodes as you need in a Godot game or application. That's how Godot gives you windows that you can pop out of the editor for dual-monitor setups, for example. That's also how the popular indie game Windowkill works (it's a cool Godot game, you should check it out!).
- Ship
- Sprite2D
- MainThruster
- GPUParticles2D
- SideThruster
- ...
_ready()
function for each node is different. To understand why, let's first see how we usually use the _ready()
function.How we usually use the _ready() function
_ready()
function is a tool to reliably set up and access child nodes from a script. We used this when connecting the Timer
node's timeout
signal in the random item placer script.func _ready() -> void:
get_node("Timer").timeout.connect(_on_timer_timeout)
_ready()
function guarantees that the Timer
node has been added to the scene tree, which is required to get a reference to it and connect its timeout
signal.When Godot calls the _ready() function for each node
_ready()
function. This time, the order of the function calls is different._ready()
function, Godot first calls the function for children nodes before calling it for their parents. It means that when the engine calls the _ready()
function for a given node, it has already called it for all its children._ready()
function of any given node._ready()
function is a great place to set up and access children nodes in your scripts, for example, for connecting signals and manipulating variables.add_child()
, the engine also calls these nodes' _ready()
functions._ready()
function after adding it.What if my script doesn't have a _ready() function?No problem! The engine has a mechanism to check if a script has a
_ready()
function and only calls it if it does. If the script doesn't have a _ready() function, the engine doesn't call it.Recap
- The
_ready()
function is a special function recognized by Godot and called when a node and all its children are ready and accessible in the code. It helps ensure nodes are fully initialized before you interact with them. - Godot creates and adds nodes to the scene tree at runtime, starting with parent nodes and moving down to their children. Godot adds nodes to the scene tree in the same order you see them in the remote scene tree.
- The engine calls the
_ready()
function for child nodes before their parents. - Whenever you instantiate a scene at runtime and add it to the scene tree using
add_child()
, Godot calls the_ready()
function for these newly added nodes.
_ready()
function.
Lesson Q&A
Use this space for questions related to what you're learning. For any other type of support (website, learning platform, payments, etc...) please get in touch using the contact form.