All examples · State and scripts

Store and persistence

What this example shows

createGameStore holds what is still true across scenes and sessions. It is split across files the way a real project would be: state.ts (plain JSON, what gets saved), two files of actions, and one index.ts that puts them together. F feeds, P plays, R resets; the bars go down over time. Reload the page and the pet is exactly where you left it.

useStore(petStore, selector, handler) reacts only when the selected value changes: the console logs the mood flip, not every frame. Saving to IndexedDB is one line, and a server of your own is an adapter with two functions, load and save.

Starting