Post

Syntactic Sugar in GDScript: Writing Cleaner Code with `@onready` and `$` ๐Ÿš€

Discover how to simplify node initialization and access in GDScript, ideal for indie game development.

Syntactic Sugar in GDScript: Writing Cleaner Code with `@onready` and `$` ๐Ÿš€

Using @onready and $ for Cleaner Code in GDScript ๐Ÿš€

In Godot, some code patterns are so common that we keep writing them over and over. One example is accessing nodes using get_node(), which is frequently used in many scripts. Fortunately, GDScript offers two forms of โ€œsyntactic sugarโ€ to simplify this: @onready and $. These shortcuts can make your code cleaner and more readable. Letโ€™s dive into how they work! ๐ŸŽฎโœจ


What is Syntactic Sugar?

โ€œSyntactic sugarโ€ refers to language features that make code easier to read and write without altering its functionality. In GDScript, @onready and $ fall into this category, and they are especially helpful for developers working with the Godot engine.


nodeds imgage

Simplifying Initialization with @onready

The @onready annotation initializes variables or nodes right before the _ready() function is called. This is useful when you want to define nodes that will only be needed once the scene is ready. Hereโ€™s an example:

Example: Before and After Using @onready

1
2
3
4
# Without @onready
var sprite2d
func _ready():
    sprite2d = get_node("Sprite2D")

In this setup, you have to initialize sprite2d within _ready(). However, by using @onready, you can simplify it as follows:

1
2
# With @onready
@onready var sprite2d = get_node("Sprite2D")

Now, the variable is initialized outside _ready(), making the code cleaner and easier to follow.


Simplifying get_node() with $

The $ symbol is a shorthand for get_node(), allowing you to write get_node("NodeName") more concisely. This is particularly helpful for readability, especially in longer code blocks.

Example: Using $

1
2
@onready var sprite2d = get_node("Sprite2D")
@onready var sprite2d = $Sprite2D  # Shorter and cleaner!


Optimizing Code with @onready and $ Together

Combining @onready and $ makes accessing nodes even easier, especially for nested paths. You can use $ to access deeply nested nodes with less typing and better readability.

Example: Combining @onready and $

1
2
@onready var animation_player = get_node("ShieldBar/AnimationPlayer")
@onready var animation_player = $ShieldBar/AnimationPlayer  # Simplified with `$`

By using $, you can keep your code straightforward, reducing potential errors that might arise from longer paths.


Practical Use Cases and Tips

@onready and $ are especially useful in the following scenarios:

  • Initializing nodes used only in _ready(): If a node is only needed in _ready(), @onready can help you set it up easily.
  • Nodes with shorter paths: $ is excellent for short paths, while get_node() can be preferable for more complex paths for readability.

Reference

๐Ÿ“˜ Godot Official Documentation - Node Path Shorthand


Conclusion

@onready and $ are great tools for simplifying code and improving readability in GDScript. These shortcuts can save time and help you write more organized scripts, especially when frequently initializing and accessing nodes. Try them out in your project today! ๐Ÿ™Œ๐ŸŒŸ

๐Ÿป Support Link for Beer-Loving Developer

For more dev tips and stories, check out my social channels!

๐ŸŒฒLink tree

If youโ€™d like to know more about me, ๐Ÿ•ต๏ธ๐Ÿป About Redping

This post is licensed under CC BY 4.0 by the author.