This document explains some choices we made in our coding style.
Mainly:
We follow many more rules and conventions to keep our code consistent and hopefully easy to read.
You can find all our code guidelines here: GDQuest GDScript guidelines.
Many of our property names have a leading underscore.
This is a convention we borrowed from Python: as GDScript doesn’t have private variables, we use a leading underscore to tell our teammates they’re pseudo-private.
In other words, a variable with a leading underscore means it’s not intended for use outside this class.
Some examples of variable names:
# We expose the speed for both read and write access.
var speed := 500.0
# The velocity is for use inside this class only.
var _velocity := Vector2.ZERO
# We store a reference to a child node, but only this script uses this
# reference.
onready var _enemy_detector: Area2D = $EnemyDetector
Technically, in GDScript, as in Python, you can always access all variables. In Godot, you can get a reference to any node inside your scene tree.
This can cause more harm than good as it gives you a lot of room to do things that’ll backfire, like accessing nodes in unintuitive ways.
At GDQuest, we use conventions like the leading underscore to limit problems, along with many other coding guidelines.
GDScript is a gradually-typed language, meaning you’re free to write fully dynamic code or to specify types for your variables and functions.
We use types as much as possible. For example, instead of
func _process(delta):
, we use
func _process(delta: float) -> void:
.
The colon after the delta
argument specifies the
variable’s type, while the arrow tells Godot the function’s return type:
here, nothing.
When defining a new variable, the GDScript compiler can often infer
the variable’s type if you write :=
.
Why bother? There are large benefits to using types:
All that for a little more typing.
Note that there’s a handy editor setting to have Godot autocomplete type hints whenever possible. To get that:
You can learn more about optional typing and its benefits in our guide to using type hints in GDScript.