Control flow
if/else, while, do, for, switch, break, continue, labels and c ? a : b
all work. switch over a number or a text tests its cases in order and falls through
without break, as in TypeScript. &&, || and ! stop as soon as the answer is known.
A condition of the game goes wherever a boolean does:
if (bring(P1, units.AnyUnit, locations.Beacon, ">=", 1) && !alarm). random() is a coin
toss and random(n) a whole number from 0 to n − 1. The game's own randomness seeds them,
so two games differ.
A loop runs to the end within the frame. while (i < 10) { …; i++; } does all ten
rounds at once. So a loop that never ends and never sleeps would never give the frame
back, and the game would freeze. The editor refuses such a loop and says where to put a
sleep(frames(1)), including a while (true) that is only left by a break.
A for whose start, bound and step are known when the script is applied, such as
for (let i = 0; i < 3; i++), is written out round by round, with i a plain value
in each copy, so createUnit(P2, unit, i + 1, at) is an ordinary action. The line's hint
says unrolled ×3. A for over a variable bound is a loop in the game. One written out
more than 256 times is an error that says how to write it as a while instead.
A for…of over a list known when the script is applied is written out once per entry, so
a wave table is ordinary data:
const waves = [{ unit: units.ZergZergling, n: 6 }, { unit: units.ZergHydralisk, n: 4 }];
program(() => {
for (const w of waves) {
createUnit(P2, w.unit, w.n, locations.Spawn);
sleep(seconds(20));
}
victory();
});
A label lets break and continue reach past the loop they are in:
program(() => {
const hp = [40, 0, 25, 0, 60, 10];
let row = 0;
let found = -1;
rows: while (row < 2) {
let col = 0;
while (col < 3) {
if (hp[row * 3 + col] > 50) { found = row * 3 + col; break rows; }
col += 1;
}
row += 1;
}
print(`${found}`); // 4
});