All examples · State and scripts

Shared data

What this example shows

One useData, made where it belongs and handed to three parts of the scene. The Hud shows it as a number, the Bar shows the same number as a width, and the Generator is the only one that changes it. Each watches on its own and none of them knows the others. Press ← and →. A signal says something happened; this is something that stays true, so a part that arrives later still reads the value it missed.

And where a store would go instead. A store belongs to the whole game: it outlives a scene change and can be saved to the browser, which is right for coins and settings and wrong for this ship's shield. useData belongs to the object, is saved with its scene and goes away with it. The other difference shows in a pause menu: useStore and useSignal keep hearing while a scene is paused, and a watch does not, because it runs with the updates.

Starting