Node creation and the ready function

In this module, we saw the _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.
We mentioned that Godot calls this function for every node in the scene tree, but we didn't go into much detail about how Godot creates nodes when running the game, in which order it adds them to the scene tree, and when it calls the function for each node.
In this study guide, we will dive into:
  • 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

When you run your game or a scene, the engine creates all the nodes required for the game to work based on the scene you run and all the scene instances it contains.
You can visualize this by opening the remote scene tree in the editor when the game runs.
To do so, head back to the editor while the game is running and click the Remote button at the top of the Scene dock.
Remote button in the Scene dock
If we take the space-level scene at the end of module 5, we can see it contains an instance of the ship, the random item placer, and the space background.
Remote scene tree
In the remote scene tree, we can expand the scene instances to see that all the nodes from their respective scenes are present at runtime and merged into a single node tree. Also, more nodes appear as the random item placer creates them.
Remote scene tree expanded

In which order Godot adds nodes to the scene tree

The tree of all the nodes in the game is called the scene tree. It's a hierarchy of nodes starting with the game window node (a viewport) that the engine automatically creates for us at the root. This node represents the game window and contains all the other nodes in the game.
Scene tree with root window node
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!).
Screenshot of the game Windowkill
When the game starts, the engine creates all the nodes required for the game to work and adds them to the scene tree.
Creating nodes happens so quickly that even with the remote scene tree, we immediately get a snapshot of the entire game's node hierarchy after creating it.
But the engine creates and adds nodes to the scene tree one at a time, from top to bottom. Parent nodes always get added before their children.
Illustration of the node creation order
So, in the ship's case, Godot creates the nodes in this order:
  • Ship
  • Sprite2D
  • MainThruster
  • GPUParticles2D
  • SideThruster
  • ...
It literally creates them in the order you see nodes in the remote scene tree, from top to bottom.
The order in which Godot calls the _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

The _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)
The _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

Once every node has been added to the scene tree, the engine makes them all "ready" and calls their _ready() function. This time, the order of the function calls is different.
For the _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.
As it's common for parent nodes to access and run code depending on their children in Godot, this order makes sure that you can put all the code to set up and access children nodes in the _ready() function of any given node.
This technical detail may be confusing now, but it will make more sense as you get more experience with Godot.
For now, remember that the _ready() function is a great place to set up and access children nodes in your scripts, for example, for connecting signals and manipulating variables.
Finally, note that whenever we create a scene instance via code and add it to the scene tree with add_child(), the engine also calls these nodes' _ready() functions.
Precisely, whenever you add a node to the scene tree, the engine calls its _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

To wrap this up, let's summarize what we learned in this guide:
  • 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.
Feel free to revisit this guide to refresh your memory about how Godot creates nodes and when it calls the _ready() function.
We'll use the function a lot in the course, so you'll also see many examples of how to use it in practice to help solidify your understanding.

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.

No questions yet! It's really ok to ask the first question.
Site is in BETA!found a bug?
The GDQuest logo

This is a testing version of our site!Please click here to go to our regular website(You will be redirected in 15 seconds)