DEV Community


Posted on

Godot Engine: GDScript

I want to share a few things that I love about the Godot Game Engine. Today I will write about gdscript.

This post is not meant as a tutorial. Check out the documentation to learn more about gdscript. In this post I will point out the things that I love about gdscript.


When I first started to use Godot I was quite sceptical about gdscript. Why create a new language when there are so many other existing languages? The answer for me is the awesome integration. It is built with the game engine in mind.

There are so many nicely integrated features! Here is a small snippet that shows some of them:

onready var detector = $DetectorArea
export var health = 10.0

remote func apply_damage(amount):
    health -= amount
Enter fullscreen mode Exit fullscreen mode

The onready keyword can be used to delay the initialization of a variable until the node is part of the scene tree. This is very helpful if you need to access another node from the scene tree for the initialization.

The $ is syntactic sugar to access a node in the scene tree by name.

The export keyword is used to make a variable editable in the editor. Example usecase:
Having many enemies with the same behaviour but different amounts of health. You can simply change the exported value for individual instances inside the editor UI!

Godot supports high level multiplayer networking. There are keywords like remote to make functions callable over the network with gdscript.


The syntax is inspired by python. As I love python it was very easy for me to adapt to gdscript. Like python gdscript also makes use of indentation.

Optional type hints

The (pretty new) type hints in gdscript are easy to use and completely optional. I often quickly prototype something without type hints. For everything beyond prototyping I use type hints wherever possible.

The editor has a concept of "safe lines". It is perfectly possible to only use types in a few places. Lines where the type check found no errors are marked as safe. This is indicated by a green line number. Lines where the type check can not be performed (because of missing type hints) do not have this green line number. Errors found by the type check are shown directly in the editor.

Often the type can be inferred (making the code much shorter).
Example: var foo := "bar" instead of var foo: String = "bar"

The match statement

I like to think of match like an enhanced version of a switch statement. Let me demonstrate some of it's awesomeness with an array named bookshelf:

match bookshelf:
        print("Empty bookshelf")
    ["The Hitchhiker's Guide to the Galaxy", ..]:
        print("The first book in your shelf is awesome!")
        print("Nothing else is important.")
    ["LOTR 1"], ["LOTR 2"], ["LOTR 3"]:
        print("You own exactly one book.")
        print("It is a book from the Lord of the Rings.")
    [var lonely_book, _ ]:
        print("There are two books.")
        print("The first book is named ", lonely_book)
Enter fullscreen mode Exit fullscreen mode

In the above match statement I used array patterns. The .. is used for open ended arrays. Using var inside a pattern binds the corresponding value to the new variable. The _ wildcard matches anything (and can be part of complex patterns).

There are many types of usable match patterns, from simple values to complex dictionary patterns!

In my opinion match is an extremely powerful structure. However it can lead to code that is hard to read. Use it with care. Think about how and why you use it. I personally love match but often simpler structures can be used. I only worked on two or three actual projects where it made sense to use it.

Thank you for reading! ✨

This is my first blog post here on DEV. My plan is to create a series of posts with things I love about Godot. If you have any questions or suggestions please let me know!

Top comments (0)