The script and the program
A program sits inside a script, and the two run at different times. The script runs once, in the editor, when it is applied. The program runs in the game. Everything the program reads from outside its body, with nothing of the program in it, is worked out when the script is applied: a constant, a helper called with known values, a condition, an action. The editor underlines those parts with dots, so you can see the line between the two halves as you type, and hovering a variable says which half it belongs to.
const spawnAt = locations.Spawn;
function burst(n: number) {
return [createUnit(P2, units.ZergZergling, n, spawnAt), displayText(`Wave of ${n}`)];
}
program(() => {
let wave = 0;
while (wave < 10) {
burst(4);
wave += 1;
sleep(seconds(20));
}
});
burst is ordinary TypeScript. It runs when the script is applied and returns two
actions, and the program runs those actions in the game. A helper that throws while the
script is applied is reported at the line that called it.
A const is what it can be. const limit = waves.length is worked out when the
script is applied. const next = wave + 1 needs a variable of the program, so it is one,
which TypeScript keeps you from assigning again. A constant inside a branch that is never
built, such as if (false) …, is never worked out at all.
The one rule to keep in mind: the player and the location of an action, and the amount
a condition compares with, are known when the script is applied. A variable of the program
cannot reach them. A parameter of a function given a known value does reach them, so
function spawn(p: Player, n: number) { createUnit(p, units.ZergZergling, n, spawnAt); }
works with spawn(P2, 4).
These parts of an action can follow a variable:
- the amount of
setResources,setDeaths,setScoreandsetCountdownTimer; - the count of
createUnit,killUnitAt,removeUnitAtandgiveUnits; - an action's unit type;
- the text of
displayTextandprint, the objectives, a leaderboard's label, a transmission and a unit type's name.
To compare a condition's amount with a variable, read the quantity and compare it
yourself: minerals(P1) >= price (Reading the game).