Control Flow and Functions
GDScript's syntax leans heavily on Python: indentation defines blocks, no curly braces, no semicolons.
var xp = 45
if xp >= 100:
print("Level up!")
elif xp >= 50:
print("Almost there")
else:
print("Keep going")
Loops
for i in range(5):
print(i)
var items = ["sword", "shield", "potion"]
for item in items:
print(item)
var tries = 0
while tries < 3:
tries += 1
for item in items iterates directly over any array-like value, exactly like Python's for x in list.
Functions
func calculate_damage(base: int, multiplier: float = 1.0) -> int:
return int(base * multiplier)
var dmg = calculate_damage(10) # 10, uses the default multiplier
var crit = calculate_damage(10, 2.0) # 20
multiplier: float = 1.0 gives the parameter a default value, callers can omit it entirely. -> int is an optional return-type annotation, purely for clarity and editor tooling, not enforced as strictly as some statically-typed languages.
TIP
Because indentation is meaningful, mixing tabs and spaces in the same GDScript file causes real parse errors, not just style warnings. Configure your editor to use one or the other consistently.