Skip to reading
PCA Script Programming

12. Break, continue and safe waiting

Edition 0.86.4.1 HF1

0.86.4.1 Hotfix 1 edition. Earlier lessons and screenshots retain their original version context; revised references and supplements describe the newer behavior.

Download PDFView PDF page
Code example
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
end

continue skips the remaining instructions in the current iteration. break leaves the nearest loop.

return leaves the function, even when it is inside several loops.

Code example
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
end

This 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.

Page 19 of 357Permalink
Go to page