scmJS docs

Owners and per-player programs

A 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. CurrentPlayer means that player.

An owner of AllPlayers, a force (players.Force1) or a list of players ([P1, P2, P3]) makes a per-player program. It runs every frame for each human or computer player among them who is in the game. CurrentPlayer is that player, and every variable, array and task is per player, each with its own copy. let total = shared(0) is one value they all share. That is how lives, scores and cooldowns are written once:

program(() => {
  let lives: u8 = 3;
  while (true) {
    if (rose(deaths(CurrentPlayer, units.TerranMarine, ">=", 1))) {
      lives -= 1;
      setDeaths(CurrentPlayer, units.TerranMarine, "set", 0);
      if (lives == 0) defeat();
    }
    sleep(frames(1));
  }
}, { owner: AllPlayers });

A script may have several programs, each with its own variables. { name: "waves" } names one, so the Explorer shows it and a test can find it. The simulator runs a per-player program one player at a time.