Writing triggers
A trigger is one call:
trigger(AllPlayers, [
bring(CurrentPlayer, units.AnyUnit, locations.Beacon, ">=", 1),
], [
displayText("You found it!"),
preserve(),
]);
trigger(players, conditions, actions, options?) records one trigger:
playersis one player or a list of them.conditionsis a list of conditions, up to 16.actionsis a list of actions, up to 64.optionsare the trigger's flags by name.{ preserve: true }is the same as thepreserve()action;disabled,ignoreGameEndand the others are listed in the Reference.
The order of the trigger() calls is the order of the triggers in the map.
Conditions and actions#
Every condition and action the game has is a function named after StarEdit's name for it,
in camel case: bring, deaths, command, accumulate, elapsedTime, createUnit,
displayText, setResources, moveUnit, order, runAiScript, victory. The one
exception is the Switch condition, which is switchIs, because switch is a reserved
word.
The arguments come in the order the Trigger Editor shows them. Where the Trigger Editor offers a list, the argument is a short word:
- a comparison is
">=","<="or"=="; - a modifier is
"set","add"or"subtract"; - a resource is
"ore","gas"or"oreAndGas"; - an order is
"move","patrol"or"attack"; - a unit count is a number or
"All".
The full list is in the Reference. StarEdit's own labels, such as
"At least", work too. Hover over any function in the editor to see its arguments.
not(condition) is the opposite of a condition, where one condition can say it:
not(bring(…, ">=", 1)) is "at most 0".
Names from the map#
Units, locations and switches are named the way the map names them:
- Each display name becomes an identifier:
Terran Marineisunits.TerranMarine, andTerran Siege Tank (Tank Mode)isunits.TerranSiegeTankTankMode. The display name works as well:units["Terran Marine"]. locations.lists the map's locations, andswitches.its switches, by number and by any name the map gives them.P1…P12,CurrentPlayerandAllPlayersare constants. The other player groups are underplayers:players.Force1,players.Foes,players.Allies.- A plain number works wherever a name does. That is how an EUD player or an unusual unit id gets in.
- With a mod's data set in use (Help ▸ Game Data…), units, weapons, upgrades and
technologies go by the mod's names instead, made identifiers as
scmjs mod headermakes them:units.Terran_Mason. See With a mod.
The editor checks the names as you type. A location passed where a unit belongs is an error, and so is a name the map no longer has.
Many triggers from a little code#
The script runs when it is applied, and everything TypeScript offers is available at that moment. A loop makes the same trigger for several players, a function returns a list of actions, a table holds the numbers, and a template string builds the text:
function reinforce(p: Player) {
return trigger(p, [deaths(p, units.TerranMarine, ">=", 10)], [
createUnit(p, units.TerranSiegeTankTankMode, 1, locations.Spawn),
setDeaths(p, units.TerranMarine, "subtract", 10),
displayText("Reinforcements have arrived."),
], { preserve: true });
}
for (const p of [P1, P2, P3, P4]) reinforce(p);
Nested lists are flattened, and false, null and undefined entries are skipped. A
helper can return a list of actions, and a condition can be written
hardMode && bring(…).
A condition is a value here: the game tests it later, when the map is played. So
if (bring(…)) outside a program does not do what it looks like, and the editor says so.
The test belongs in a trigger's conditions, or in an if inside a program.
Several files#
A script can have several files. import { x } from "./name" brings in another file of
the map's script; nothing else can be imported. The library is available everywhere
without an import, and also as the module "trigscript" for anyone who prefers
import { trigger, bring } from "trigscript".
Hyper triggers#
hyperTriggers(P8) anywhere in the script adds the usual three preserved triggers of
sixty-two waits, so that the game goes through the trigger list every 2 frames instead of
every 31 (Hyper triggers). Give them to a player whose other
triggers never wait, usually a computer slot: each player has one wait slot, and the hyper
triggers keep it busy (Waits). A map with a program does not need
them, because its build already makes the list run every frame. The build leaves hyper
triggers out, both the script's and the map's own, and says so in a notice. It does the
same for a trigger owned by a player group the game runs no list for (Players 9 to 12,
Foes, Allies, Neutral, Non-Allied Victory Players).