---
url: /api/kiln/functions/state.md
---
[Kiln API](../../index.md) / [kiln](../index.md) / state

# Function: state()

```ts
function state<S>(values, initial?): State<S>;
```

Defines a state: `export const Screen = state(['menu', 'playing', 'paused', 'gameover'], 'menu')`.
Register it with `app.init(Screen)`. Gate systems with `runIf(inState(Screen, 'playing'), ...)`,
run one-off setup with `runIf(onEnter(Screen, 'playing'), SpawnLevel)` in Update or Fixed, and
change it with `res.mut(Screen).set('gameover')`. Transitions apply at the start of the next
frame; entities spawned with `despawnOnExit(Screen, v)` are despawned when `v` is left.

## Type Parameters

| Type Parameter |
| ------ |
| `S` *extends* `string` |

## Parameters

| Parameter | Type |
| ------ | ------ |
| `values` | readonly `S`\[] |
| `initial` | `S` |

## Returns

[`State`](../interfaces/State.md)<`S`>
