Signals
A Signal is an event of your own. One part of a map says that something happened, such
as a wave cleared, without knowing who listens: the shop, the score and the music each add
a handler, and the wave code only emits.
import { Signal } from "trigscript/signals";
program(() => {
const waveCleared = new Signal<number>();
waveCleared.on((wave) => { setResources(P1, "add", 100 * wave, "ore"); });
waveCleared.once(() => print("First wave down!")); // First wave down!
const id = waveCleared.on((wave) => print(`Wave ${wave} cleared`)); // Wave 1 cleared
waveCleared.emit(1);
waveCleared.off(id);
waveCleared.emit(2);
});
A signal carries one value of the type it is made with: a number, a boolean, a text or a
unit (new Signal<Unit>()). Its handlers are functions kept as
values, and they run one after another, in
the order they were added, within the frame. A handler that runs an action is written with
braces, (wave) => { setResources(…); }, since a kept function cannot give back an
action. Make the signal inside the program that uses it.
| Name | What it does |
|---|---|
new Signal<T>() |
A signal carrying a T. |
on(handler) |
Add a handler for every emit. Gives its number, for off. |
once(handler) |
Add a handler for the next emit only. |
off(id) |
Take a handler off. |
emit(value) |
Run every handler with value. |
count(), clear() |
How many handlers it has; take them all off. |