If Else in GDScript
Branching with if
and else
in GDScript is straightforward.
Note that you don’t need parentheses around conditions in GDScript, but the colon :
at the end of the condition and proper indentation are required.
To run this script in Godot:
- Create a new script and paste this code into the
_ready()
function. - Attach the script to a node in your scene.
- Run the scene.
The output will be:
In GDScript, there is no ternary operator like in some other languages. You’ll need to use a full if
statement even for basic conditions.