# Data and watch

> useData is state a piece of the scene owns, read as lives.value; useWatch runs when it changes and once on the first frame.

Example `data-watch`, running at https://nacatamalon.com/en/examples/data-watch. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`useData` is state a piece of the scene owns, read as `lives.value`; `useWatch` runs when it changes. Press **Space** to take a hit and **R** to reset. The setter takes three forms, all here: the value itself, a function that returns the next one, and a function that changes it in place. The watch runs once on the first frame too, so the screen starts right without being written twice. Change it with `lives.value = 0` instead and the watch will not hear about it: the same rule React has.

**When not to reach for it.** For a value only this component uses, a plain `let` in the body is enough: the body runs once, but everything written inside it sees the same variable afterwards. What `useData` adds are two things a local variable cannot do: it is written into the scene document, so a saved game gives the value back; and it has an identity, so it can be handed to another part of the scene, which is the next example.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Level } from './scenes/demo';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#14121c',
});

// Declare Scenes
game({
    Level
});
```

`components/counter.ts`:

```ts
import { createSprite, createText, useData, useKeyboard, useLoadBitmapFont, useUpdate, useWatch } from 'nacatamalon';
import * as Settings from '../settings';

export const Counter = () => {
    const font = useLoadBitmapFont(Settings.FONT);
    const keys = useKeyboard();

    // The state this piece of the scene owns. A box and not a number: the body runs once, so a
    // number read here would be 3 for ever no matter what happened afterwards.
    const [lives, setLives] = useData(Settings.LIVES);

    // A second one, to show the third form of the setter.
    const [run, setRun] = useData({
        hits: 0
    });

    const hero = createSprite({
        tint: Settings.ALIVE,
        width: 56,
        height: 56,
        transform: {
            x: 142,
            y: 96
        }
    });

    const counter = createText({
        text: '',
        font,
        style: {
            fontSize: 16
        },
        tint: Settings.PAPER,
        transform: {
            x: 24,
            y: 24
        }
    });

    const hits = createText({
        text: '',
        font,
        style: {
            fontSize: 8
        },
        tint: Settings.PAPER,
        transform: {
            x: 24,
            y: 52
        }
    });

    createText({
        text: 'SPACE: HIT    R: RESET',
        font,
        style: {
            fontSize: 8
        },
        tint: '#8fa4c4',
        transform: {
            x: 24,
            y: 176
        },
    });

    // Runs on the first frame even though nothing has changed yet, which is why the screen starts
    // right without writing its starting state twice.
    useWatch(() => {
        counter.text = 'LIVES ' + lives.value;
        hero.tint = lives.value > 1 ? Settings.ALIVE : Settings.HURT;
        hero.visible = lives.value > 0;
    }, [lives]);

    // Its own watch: this one only cares about the count of hits.
    useWatch(() => {
        hits.text = 'HITS ' + run.value.hits;
    }, [run]);

    useUpdate(() => {
        if (keys.justPressed(' ')) {
            // Gives back the next value.
            setLives((n) => Math.max(0, n - 1));
            // Changes what it was handed and gives back nothing. Both count as a change.
            setRun((current) => {
                current.hits += 1;
            });
        }

        if (keys.justPressed('r')) {
            // The value itself, for when there is nothing to work out.
            setLives(Settings.LIVES);
            setRun({
                hits: 0
            });
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';

import { Counter } from '../components/counter';

export const Level = () => {
    useSpawn(Counter)();

    return createScene();
};
```

`settings.ts`:

```ts
import { getColor } from 'nacatamalon';

export const WIDTH = 340;
export const HEIGHT = 200;
export const LIVES = 3;
export const FONT = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png',
};
export const ALIVE = getColor('#5bd75b');
export const HURT = getColor('#e5533b');
export const PAPER = getColor('#f2f6fa');
```
