TrigScript language
What a program can say: numbers, arrays, maps, functions, classes, errors, tasks, units, events, input, text, sounds and libraries, and where it differs from TypeScript.
This is the language of a TrigScript program: what the code inside program(() => { … })
can say, and what it does in the game. Start with the TrigScript guide if
you have not written a script yet. It has the quick start, trigger(), the workspace and
testing. Come here when a program needs something the guide only mentions, or when you
know TypeScript and want to know where a program is different.
A script has two halves. Outside program(), the script is TypeScript: it runs when the
script is applied, with all of the language and its standard library, and what it records
is ordinary triggers. Inside program(), every line has to become something the game can
do while the map is played. Most of TypeScript comes through unchanged. These pages say
what each part does, and what to write instead of the few parts that are not there.
| Page | What is on it |
|---|---|
| TrigScript beside TypeScript | The whole comparison in one table |
| The script and the program | What is worked out when the script is applied, and what runs in the game |
| Variables and numbers | Numbers, booleans, records, undefined; u8, u16, u32; fractions and Math |
| Arrays | Fixed and growing arrays; arrays of records, units, texts and arrays; the methods; patterns and spread |
| Tables, maps and sets | Lists the script made, tables keyed by an id of the game, Map and Set |
| Control flow | Loops within the frame, unrolled loops, labels, switch |
| Functions | Inlined and called functions, functions that call themselves, game(), functions as values and closures |
| Classes | Fields, methods, extends, and how an instance is kept |
| Errors | try, catch, finally, throw and classes of error |
| Waiting and tasks | sleep, async and await, tasks, Promise.all and race, generators, edges |
| Owners and per-player programs | One program for every player, and shared() |
| Reading the game | Every condition's quantity as a number; facts about the players |
| Units | Loops over units, picks, a unit's fields and what it can be told |
| Events | on.created, on.died, on.killed, on.morphed, on.order |
| The game's tables and memory | stats() for costs, weapons, upgrades and colours; memory |
| Keys, the mouse and chat | Presses, clicks, the cursor, typed lines; local and sync() |
| Text | Texts with values, string variables and their methods, lines of JSX |
| Sounds and imported files | Clips made when the script is built; videos, pictures, sounds and MIDI the script imports |
| Libraries | The standard library's nine modules, and libraries other plugins provide |
| What a program does not have | What is refused, and what to write instead |
Every example on these pages compiles as it is written. A line that ends with a comment
such as // 80 prints that when the example runs. Examples that name locations assume the
map has them: Beacon, Spawn, Base, Pen, Gate, Exit and Cursor.
- TrigScript beside TypeScriptThe table is about the inside of a program.
- The script and the programA program sits inside a script, and the two run at different times.
- Variables and numbersVariables hold numbers, booleans, texts, units and players.
- ArraysArrays hold numbers, booleans, texts, units, records, instances of a class, functions or other arrays.
- Tables, maps and setsA list the script made is a table a program can look things up in.
- Control flowif/else, while, do, for, switch, break, continue, labels and c ? a : b all work.
- FunctionsArguments pass by value, as in TypeScript.
- ClassesClasses are TypeScript's, declared inside the program or at the top of any of the script's files.
- Errorstry, catch, finally and throw work as in TypeScript.
- Waiting and tasksA program runs every frame, from where it left off.
- Owners and per-player programsA program runs for its owner, Player 1 unless { owner: … } says otherwise, as a trigger would: as that player, and only while that player is in the game.
- Reading the gameEvery quantity a condition compares is also a value.
- UnitsThe units on the map are objects.
- Events**on.* runs a function each time something happens to a unit**, from the frame it is written on.
- The game's tables and memorystats() reaches the game's own tables: what a unit type costs, what a weapon does, a player's upgrades.
- Keys, the mouse and chatWhat the players do is keyPressed, keyHeld, clicked, mouse and chatted.
- TextdisplayText and print take a template literal, or texts joined with +, with the program's numbers in it, name(p) and color(p):
- Sounds and imported filesplayWav(sound, 0) plays a sound: one of the game's own files by its path, one of the map's sounds by its name (sounds.Name, from the Sound Editor's list), or a clip the script makes when …
- LibrariesTrigScript comes with nine modules written for programs, imported as "trigscript/<name>".
- What a program does not haveOutside program() the script is all of TypeScript, since it simply runs.