Programs
Everything inside program(() => { … }) runs in the game:
program(() => {
let wave = 0;
let alarm = false;
while (true) {
if (bring(P1, units.AnyUnit, locations.Beacon, ">=", 1) && !alarm) {
alarm = true;
displayText("They are coming.");
}
if (alarm) {
createUnit(P8, units.ZergZergling, 4, locations.Spawn);
wave += 1;
}
if (wave >= 10) defeat();
sleep(seconds(20));
}
}, { owner: P1 });
A map with a program needs StarCraft: Remastered. The programs are built into the map when it is saved or tested (see Saving and building).
Note. Building a program also changes how the rest of the map runs: the game goes through the trigger list every frame instead of every 31 frames. A preserved trigger that adds a mineral then adds twenty-four a second at Fastest.
What is worked out when the script is applied#
This is the one rule to keep in mind. Everything the program reads from outside its body is worked out once, when the script is applied, before the game starts: a constant, a helper, a condition, an action. The editor underlines those parts with dots, so you can see the boundary as you type. That is what lets a helper written outside the program supply actions inside it.
In the picture, the lines beside the gold bar run once in the editor, and the lines beside
the blue bar run in the game, where price is the number 150 and reward(P1) is the
action the helper made.
It also means a program variable cannot reach a condition, an action or a helper. The
location and the player of an action, and the amount a condition compares with, are fixed
when the script is applied. To compare with a variable, read the quantity and compare it
yourself: minerals(P1) >= price (see Reading the
game).
These parts of an action can follow a variable:
- the amount of
setResources,setDeaths,setScoreandsetCountdownTimer; - the unit count of
createUnit,killUnitAt,removeUnitAtandgiveUnits; - an action's unit type;
- a text:
displayTextandprint, the objectives, a leaderboard's label, a transmission and a unit type's name take any text the program made.
A sound's path or the next scenario's name is a text written in the script.
How a program runs#
A program runs every frame, from where it left off. It starts on frame 2, the game's
first pass. Its body runs until it reaches a sleep() or its end, all within one frame of
the game, and the next frame it carries on from there. A body that ends stops for good. So
the loop above is a game loop: it looks at the beacon, sends a wave if the alarm is up,
and sleeps twenty seconds.
In the picture, a program that sleeps three frames at a time runs on frames 2, 5 and 8, while one that sleeps a frame at a time runs on every frame.
Time is sleep. sleep(seconds(15)) gives the frame back and carries on that much
later, while other programs and the map's triggers go on. frames(n) and minutes(n)
make a duration too. Something that keeps its own time is a task beside the program, or
another program (Waiting and tasks).
The game's own wait() is allowed in a program, but it is a different thing: it holds the
rest of its trigger for the time, and it uses the player's one wait slot, so any other
Wait of that player stops until the slot is free (Waits). Use it for
a short pause, such as a text and then a sound, and use sleep to pass time.
Two kinds of second. A second of sleep(seconds(n)) is 24 frames, which is one real
second at Fastest. A game second is 16 frames at every speed. elapsed(), countdown()
and the Elapsed Time and Countdown Timer conditions count game seconds, so at Fastest they
run about one and a half times as fast as sleep seconds: after sleep(seconds(14)),
elapsed() reads about 21.
Edges. if (rose(bring(…))) is true on the frame the condition becomes true, and not
again until it has been false in between. once(…) is true the first time only.
random() is a coin toss, and random(n) a whole number from 0 to n − 1.
Owners and per-player programs#
A program runs for its owner, as that player, while that player is in the game. The
owner is Player 1 unless { owner: … } says otherwise.
An owner of AllPlayers, a force or a list of players makes a per-player program. The
same code runs for each of those players who is in the game, computers included.
CurrentPlayer is that player, and every variable is per player, each with its own copy.
That is how lives, scores and cooldowns are written once. let total = shared(0) is one
value that all of them share.
{ name: "waves" } names a program. The name appears in the Explorer, and a
test finds the program by it.
What a program can use#
A program is written in nearly all of TypeScript. The TrigScript language guide covers each part in full. In short:
- Values: numbers, booleans, texts, units, players and records.
u8,u16andu32are numbers that are never below zero, and a number holds a fraction once one is stored in it (Variables and numbers). - Arrays, maps and sets, fixed or growing, with their methods (
map,filter,sortand the rest), patterns and spread (Arrays, Tables, maps and sets). - Functions and classes, including functions kept in variables, closures,
extendsandtry/catch(Functions, Classes, Errors). - Time:
sleep,asyncandawait, tasks that run beside the program, andPromise.allandrace(Waiting and tasks). - The game: every condition's quantity as a number, loops over units and their fields,
events such as
on.died, and the game's own tables throughstats()(Reading the game, Units, Events, The game's tables and memory). - The players: keys, clicks, the cursor and typed lines (Keys, the mouse and chat).
- Text and sound: texts with values, lines of JSX, and sounds the script makes (Text, Sounds and imported files).
- Libraries: eleven modules that come with TrigScript, such as
trigscript/uifor menus andtrigscript/cutscenefor scenes, and libraries other plugins provide (TrigScript library).
A few of these together: a wave every thirty seconds, each bigger than the last, until Player 1 has lost ten Marines.
import { every, SECOND } from "trigscript/timers";
program(async () => {
let lost = 0;
on.died({ owner: P1 }, (u) => {
if (u.type == units.TerranMarine) lost += 1;
});
const waves = every(30 * SECOND, (n) => createUnit(P8, units.ZergZergling, n * 2, locations.Spawn));
await until(() => lost >= 10);
waves.cancel();
displayText("Ten Marines lost.");
defeat();
});
What a program cannot do, and what to write instead, is listed under What a program does not have.