GD Collection

Control flow

Programs often need to take one action in certain cases, and different actions in different cases.
Also, programs often need to repeat actions multiple times.
These two ideas are called control flow.
table by: https://generalistprogrammer.com/cheatsheets/godot-gdscript

Code Description Example
if condition: condicional statement if health <= 0: die() elif health < 20: play_low_health_effect() else: play_normal_animation()
for i in range(n): loop n times for i in range(5): spawn_enemy() for i in range(10, 20): print(i)
for item in array: Iterate over array for enemy in enemies: enemy.update() for key in dictionary: print(key, dictionary[key])
while condition: while loop while is_alive: update() await get_tree().process_frame
match value: Pattern matching (switch statement) match state: "idle": idle_behavior() "running": run_behavior() _: print("Unknown state")
break exit loop early for i in range(100): if found: break
continute Skip to next iteration for enemy in enemies: if enemy.is_dead: continue enemy.attack()