GDScript Fundamentals

Lesson 3 of 7

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.

📝 Control Flow and Functions Quiz

Passing score: 70%
  1. 1.What defines a code block in GDScript?

  2. 2.A function parameter can be given a default value, making it optional for callers.

  3. 3.The keyword used to define a function in GDScript is: ____ calculate_damage(base):