Signals
Back to the reference

Signals

Named event channels, and the events an object sends up to whoever holds it.

7 symbols

createGameSignalfunctionnacatamalon

createGameSignal() => TGameSignal<T>

Makes a signal: a way for one part of the game to say "this happened" and for any other part to react, without the two knowing about each other.

A button does not need to know about the screen that lights up, and the screen does not need to know about the button: both know the signal. That is what keeps them apart, so either can be changed, removed or duplicated without touching the other.

Make it once, in a file of its own, and import it wherever it is needed. It belongs to the page, not to a game, so it can be fired from outside the canvas too (a web page button, a tool).

  • emit(value) tells everyone listening, straight away.
  • useSignal(signal, handler) listens from a scene, and stops by itself when the scene goes.
  • connect(handler) listens from anywhere else, and returns the function that stops.

A signal carries events, not state: nothing is kept after emit. What should still be true later (a score, a level reached) belongs in the game's state, not here.

Example

let score = 0;

// events.ts
export const coinCollected = createGameSignal<number>();

// coin.ts
coinCollected.emit(10);

// hud.ts, inside a scene
useSignal(coinCollected, (points) => { score += points; });
View source · signal/create_game_signal.ts:41

onReportfunctionnacatamalon

onReport(object: TBox, name: string, handler: TReportHandler<T>) => () => void

Hears what a piece reports to whoever placed it: a pack's button pressed, a behaviour's box out of lives. The piece sends it with useReport; it climbs from there to the first object that listens, so listening on a whole copy hears any part of it.

For pieces made of data, a pack or a behaviour, which cannot be handed a function. A component of your own code takes a callback in its props instead (useSpawn(Enemy)({ onDied })); something the whole game hears is a signal.

Stops when the object goes away, and, called while something is being built, when that something does. The function it returns stops it earlier.

Parameters

objectTBox

What to listen on: the report reaches it from itself or from anything inside it.

namestring

The report, as the piece sending it named it.

handlerTReportHandler<T>

What to do, given what came with it and the object that sent it.

Example

declare const copy: TGameObject;

onReport<{ label: string }>(copy, 'pressed', ({ label }) => {
    console.log(label);
});
View source · events/on_report.ts:35

useReportfunctionhooknacatamalon

useReport(name: string) => (payload: T) => boolean

Lets a piece report to whoever placed it, and gives back the function that sends the report: a pack's button saying it was pressed, a behaviour saying its box is out of lives. It climbs to the first object above that listens (onReport, or on in createPack) and stops there. Answers whether anybody heard it.

For pieces made of data, a pack's script or a behaviour, which cannot be handed a function. A component of your own code takes a callback in its props instead; something the whole game hears is a signal.

Parameters

namestring

What the report is called. The one who listens uses the same name.

Example

registerScript('button', (self, props) => {
    const pressed = useReport<{ label: string }>('pressed');
    listen(self, { onClick: () => pressed({ label: String(props.label) }) });
});
View source · hooks/events/use_report.ts:29

useSignalfunctionhooknacatamalon

useSignal(signal: TGameSignal<T>, handler: TSignalHandler<T>) => void

Listens to a signal from a scene, and stops listening by itself when that part of the scene goes away. Nothing to disconnect by hand.

Call it in the scene body, or in the body of something created with useSpawn: then it stops when that object is destroyed. It keeps listening while the scene is paused, because a signal is the game telling itself something, and a paused level may still need to hear it.

Parameters

signalTGameSignal<T>

The signal, made with createGameSignal.

handlerTSignalHandler<T>

What to do each time it fires. Receives what the signal carries.

Example

declare const coinCollected: TGameSignal<number>;

export const Hud: TSceneFn = () => {
    let score = 0;
    useSignal(coinCollected, (points) => { score += points; });
    return createScene();
};
View source · hooks/signal/use_signal.ts:31

TGameSignaltypenacatamalon

A channel one part of the game fires and any other part listens to, without either knowing the other exists. Made with createGameSignal.

Properties

emit
connect
sizenumber

How many are listening. Useful to spot a listener that was never disconnected.

View source · signal/types/t_game_signal.ts:18

TReportHandlertypenacatamalon

TReportHandler: (payload: T, from: TBox) => void

What a handler connected with onReport is told: what came with the report, and the object that sent it (the button itself, inside the copy a game placed).

View source · events/reports.ts:12