scmJS docs

Examples of triggers

Each of these is a complete main.ts, and each plays on every version of the game. They assume a map with locations named Beacon, Spawn, Base, Hill and Shop. Use your own names; the editor completes them.

Starting resources and a welcome. No preserve, so the trigger runs once, and CurrentPlayer means each player in turn.

trigger(AllPlayers, [always()], [
  setResources(CurrentPlayer, "set", 500, "ore"),
  setResources(CurrentPlayer, "set", 100, "gas"),
  displayText("Hold the hill for five minutes to win."),
]);

Hyper triggers. One line, and the whole trigger list runs every 2 frames. Player 8 is the computer slot here, and nothing else in the script makes that player wait.

hyperTriggers(P8);

Something for every player. A helper returns the trigger, and a loop calls it. Death counts are the game's own counters. Subtracting ten after the reward lets the trigger fire again at the next ten, which preserve: true allows.

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);

A shop. Bring a civilian to the shop with enough minerals, and four marines appear at the spawn. The civilian is moved out first, so the trigger does not fire again on the next pass. price is an ordinary constant used twice.

const price = 150;

trigger(AllPlayers, [
  bring(CurrentPlayer, units.TerranCivilian, locations.Shop, ">=", 1),
  accumulate(CurrentPlayer, ">=", price, "ore"),
], [
  moveUnit(CurrentPlayer, units.TerranCivilian, 1, locations.Shop, locations.Spawn),
  setResources(CurrentPlayer, "subtract", price, "ore"),
  createUnit(CurrentPlayer, units.TerranMarine, 4, locations.Spawn),
  displayText(`Four marines for ${price} minerals.`),
], { preserve: true });

A map that already has triggers. Open TrigScript, press … and choose Import the Map's Triggers. Every hand-made trigger comes back as a trigger() call, in its order, and applying the script replaces the whole list with what it makes. From there a repeated trigger becomes a loop, a number used in ten places becomes a constant, and the rest stays as it was.