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.
on.created(fn): every unit that comes onto the map, handed over on the next frame.on.died(fn): every unit that dies or is removed, handed over as it was.on.killed((u, by) => …): every unit killed in a fight, with the player whose attack killed it.on.morphed((u, was) => …): a unit that turns into another type and stays the same unit, with the type it was.on.order((u, order, was) => …): a unit that takes up a new order, with the one it had. This compares every unit's order with the last frame's, so it costs a look through the units every frame.
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);
});
});