That script is attached to a CanvasLayer node with a ProgressBar called HealthBar. And yet, when running the scene, it will throw an error:
This is because there’s actually a spelling error; instead of HealthBar, the node is called HeatlhBar. It’s easy to miss, and we all occasionally make spelling errors.
Since you can easily drag nodes into scripts and create @onready references by holding down CTRL before releasing the mouse button, another version of the script might look like this:
With the script in place, the
health_bar
node can be assigned via the Inspector:
Initially, this may seem like a bit more work, to first have to declare it and then assign it, but it is the most reliable, flexible solution to referencing nodes. You can even rename the HealthBar node, and the
health_bar
node reference will update accordingly.
This approach is especially helpful when building complex user interfaces where you might rearrange nodes a lot. One concern is having a long list of export properties, some of which are actually used to reference nodes internal to the scene. That’s why I typically group them with
@export_group
:
That way, it’s clear in the inspector which properties are meant to be modified from outside the scene:
In addition, this separation between script and scene is a form of dependency injection that provides greater flexibility, so a script can be attached to a scene that has a completely different layout.
I’ve been using this technique almost exclusively, and rarely use
@onready
to assign a node reference via
$
or
get_node()
.
Godot Engine is an open-source game engine. With the 4.0 release on the horizon, it’ll gain quite a bit of attention. And it’s an engine worth keeping an eye on. Internet Gaming. Serious business. Game development is serious business. The global gaming market size was 203 billion USD in 2020 (per fortunebusinessinsights). It is predicted …
Arguably, more fun than writing code is removing code. I was assembling a split-screen multiplayer UI. The goal behavior is to show/hide the appropriate displays for the players, depending on how many players there are. Initially, the code to update the UI was very simple, because I started with two players. In that case, you …
Here’s a tutorial on how to create breakable objects in Blender and Godot. It covers the steps needed to design and implement breakable objects, including scripting and using physics properties to make objects break apart into smaller pieces upon collision or other interactions.
This release finally uses Godot Engine 4.4. It adds the GGCraftingSystem singleton and updates the GGInteractable2DStrategyCrafting class to use it. The crafting editor nodes now have prefixes, which makes it much easier to search for specific recipe or item nodes in larger crafting libraries. Some syntactic sugar was added as well. You can now easily …
Ditch @onready, use @export instead
Are you using @onready to reference nodes? There’s a better way!
Here’s a simple example of how many tutorials use @onready to reference nodes:
That script is attached to a CanvasLayer node with a ProgressBar called HealthBar. And yet, when running the scene, it will throw an error:
This is because there’s actually a spelling error; instead of HealthBar, the node is called HeatlhBar. It’s easy to miss, and we all occasionally make spelling errors.
Since you can easily drag nodes into scripts and create @onready references by holding down CTRL before releasing the mouse button, another version of the script might look like this:
Now it works, because it’s consistently misspelled.
If you now notice the misspelling in the Scene Tree and fix the node name, but don’t adjust the script, it’ll break again:
A better way is to use @export instead:
With the script in place, the
health_barnode can be assigned via the Inspector:Initially, this may seem like a bit more work, to first have to declare it and then assign it, but it is the most reliable, flexible solution to referencing nodes. You can even rename the HealthBar node, and the
health_barnode reference will update accordingly.This approach is especially helpful when building complex user interfaces where you might rearrange nodes a lot. One concern is having a long list of export properties, some of which are actually used to reference nodes internal to the scene. That’s why I typically group them with
@export_group:That way, it’s clear in the inspector which properties are meant to be modified from outside the scene:
In addition, this separation between script and scene is a form of dependency injection that provides greater flexibility, so a script can be attached to a scene that has a completely different layout.
I’ve been using this technique almost exclusively, and rarely use
@onreadyto assign a node reference via$orget_node().Related Posts
Is Godot is the Linux of Game Engines?
Godot Engine is an open-source game engine. With the 4.0 release on the horizon, it’ll gain quite a bit of attention. And it’s an engine worth keeping an eye on. Internet Gaming. Serious business. Game development is serious business. The global gaming market size was 203 billion USD in 2020 (per fortunebusinessinsights). It is predicted …
A GDScript refactoring exercise
Arguably, more fun than writing code is removing code. I was assembling a split-screen multiplayer UI. The goal behavior is to show/hide the appropriate displays for the players, depending on how many players there are. Initially, the code to update the UI was very simple, because I started with two players. In that case, you …
Making breakable objects in Godot
Here’s a tutorial on how to create breakable objects in Blender and Godot. It covers the steps needed to design and implement breakable objects, including scripting and using physics properties to make objects break apart into smaller pieces upon collision or other interactions.
Inventory System 2 Alpha 4 available
This release finally uses Godot Engine 4.4. It adds the GGCraftingSystem singleton and updates the GGInteractable2DStrategyCrafting class to use it. The crafting editor nodes now have prefixes, which makes it much easier to search for specific recipe or item nodes in larger crafting libraries. Some syntactic sugar was added as well. You can now easily …