12. Break, continue and safe waiting
function run()
let found = []
for each n in range(0, 10)
if n == 2
continue
end
if n == 5
break
end
found = found + [n]
end
return found
endcontinue skips the remaining instructions in the current iteration. break leaves the nearest loop.
return leaves the function, even when it is inside several loops.
global panel_open = false
function run()
let attempts = 0
while not panel_open and attempts < 50
wait 0.1 seconds
attempts += 1
end
return panel_open
endThis waits for up to roughly five seconds without busy-looping. wait until panel_open is shorter, but can wait indefinitely if nothing sets the flag. Use a bounded loop when failure to arrive needs a useful response. wait uses seconds; there is no need to write GDScript’s await around PCA instructions.
