scmJS docs

Events

on.* runs a function each time something happens to a unit, from the frame it is written on. const t = on.died(…) gives back a task, and t.cancel() stops listening. An async function is started as a task each time, so it may wait.

Each takes a unit type or a filter first: on.created(units.ZergZergling, …), on.died({ owner: P8 }, …), on.killed({ owner: P8, by: P1 }, …). A listener written at the top of a file, outside any program, listens from the start of the game. on.key and on.chat do the same for keys and typed lines (Keys, the mouse and chat).

program(async () => {
  let lost = 0;
  on.died({ owner: P1 }, (u) => {
    lost += 1;
    if (u.type == units.TerranMarine) print(`${lost} lost`);
  });
  on.killed((u, by) => { if (by == P1) setResources(P1, "add", 5, "ore"); });
  on.created(units.ZergZergling, (u) => { u.hp = u.maxHp / 2; });
  on.died({ owner: P8 }, async (u) => {
    await sleep(seconds(2));
    createUnit(P8, u.type, 1, locations.Spawn);
  });
});