# RoomDefinition

> The RoomDefinition interface of the Room API is a room's code, the object passed to defineRoom().

The **`RoomDefinition`** interface of the [Room API](/docs/room) is a room's code: the object passed to [`defineRoom()`](/docs/room/defineroom), which a room module exports as its default. It holds the room's settings, such as its schema and tick rate, and the hooks the room host calls as the room runs.

Every member is optional. A room module is one room type, and every room of that type runs the same definition. The room host calls each hook with the [`Room`](/docs/room/room) it's running for.

A definition is either a room with ticks, which has a `tickRate`, or a room without them. Only a room with ticks may have `stepper`, `history` and `onTick`; see [With or without ticks](/docs/room/sdk#with-or-without-ticks).

## Instance properties

[`RoomDefinition.schema`](/docs/room/room-definition/schema)
: The room's [`Schema`](/docs/room/schema), shared with its clients. Without one, the room has no entities or calls.

[`RoomDefinition.tickRate`](/docs/room/room-definition/tickrate)
: Ticks per second, an integer from 1 to 120. Without one, the room has no ticks.

[`RoomDefinition.stepper`](/docs/room/room-definition/stepper)
: The simulation step the room and its clients share. Needs a `tickRate`.

[`RoomDefinition.interest`](/docs/room/room-definition/interest)
: Decides which entities each connection sees. Without it, every connection sees every entity.

[`RoomDefinition.voice`](/docs/room/room-definition/voice)
: Turns on proximity voice and decides who hears whom.

[`RoomDefinition.history`](/docs/room/room-definition/history)
: The fields the room records each tick for `room.rewind()`. Needs a `tickRate`.

[`RoomDefinition.data`](/docs/room/room-definition/data)
: Makes `room.data` each time the room starts or wakes.

[`RoomDefinition.version`](/docs/room/room-definition/version)
: The room state version. Defaults to 1.

[`RoomDefinition.migrations`](/docs/room/room-definition/migrations)
: Up migrations of room state, keyed by the version each one produces.

[`RoomDefinition.privateRetention`](/docs/room/room-definition/privateretention)
: How long a player's private state is kept after the player was last connected. Defaults to 24 hours.

[`RoomDefinition.sleep`](/docs/room/room-definition/sleep)
: Puts the room to sleep once it has been quiet for a time, with its players still connected.

[`RoomDefinition.bots`](/docs/room/room-definition/bots)
: The behaviours the room's bots run, by name.

## Hooks

[`onCreate()`](/docs/room/room-definition/oncreate)
: Runs once, when the room starts with no room state.

[`onWake()`](/docs/room/room-definition/onwake)
: Runs when the room starts from room state, after hibernating or after its room host restarted.

[`onAlarm()`](/docs/room/room-definition/onalarm)
: Runs when the room's alarm is due, whether the room is active or hibernated.

[`onJoin()`](/docs/room/room-definition/onjoin)
: Runs when a connection opens, including each reconnect.

[`seesPresence()`](/docs/room/room-definition/seespresence)
: Decides whether one connection sees another player's presence entry.

[`onMessage()`](/docs/room/room-definition/onmessage)
: Runs for each message from a connection.

[`onLeave()`](/docs/room/room-definition/onleave)
: Runs when a connection closes, and for each open connection before the room hibernates.

[`onPlayerLeave()`](/docs/room/room-definition/onplayerleave)
: Runs once when a player's presence ends.

[`onTick()`](/docs/room/room-definition/ontick)
: Runs each tick, after the stepper. Needs a `tickRate`.

[`onHibernate()`](/docs/room/room-definition/onhibernate)
: Runs before the room saves its room state and leaves its room host.

[`onRequest()`](/docs/room/room-definition/onrequest)
: Answers a message from the server API.

[`calls`](/docs/room/room-definition/calls)
: A handler for each call the schema declares.

[`writes`](/docs/room/room-definition/writes)
: A handler for each kind with writable fields, which decides what an owner write applies.

## When a hook throws

What happens depends on the hook. A hook's promise that rejects counts as a throw.

| Hook | If it throws |
|---|---|
| `onCreate`, `onWake`, `onTick`, `onHibernate` | The room stops. |
| `onAlarm` | It runs again after 2, 4, 8, 16, 32 and 64 seconds, then the alarm is dropped. The error is logged and the room keeps running. |
| `onJoin` | That connection closes with close code 4006. The room keeps running. |
| `onMessage` | The error is logged with the room, player and tick, and the message is dropped. The room keeps running. |
| `onLeave`, `onPlayerLeave` | The error is logged. The room keeps running. |
| `onRequest` | The server API call gets a `422 room_error` reply with the error's message. The error is logged unless it's a `CallRejected`. |
| `calls[name]` | The client's call rejects with the error's message. The error is logged unless it's a `CallRejected`. The room keeps running. |
| `writes[kind]` | The error is logged, the write is rejected and the owner corrected. |

A room that stops reports the error and closes its connections with close code 4007. Other rooms in the same room host keep running. An error in the stepper, in an interest function, in `seesPresence` or in a migration also stops the room.

`onCreate`, `onWake` and `onHibernate` may be async: the room starts, or hibernates, once their promise resolves. `onRequest` and call handlers may be async too, and their reply is what the promise resolves to. The other hooks may return a promise, but the room doesn't wait for it.

## Order within a tick

In a room with ticks, each tick:

1. Applies owner writes through `writes`.
2. Runs the stepper with this tick's inputs.
3. Runs `onTick`.
4. Sends the state update to every connection.
5. Acknowledges the inputs it applied.

Messages and calls are handled as they arrive, between ticks, and their changes go out with the next tick's state update. A connection's owner writes that came before its message or call are applied first, through `writes`, so the handler sees what the player wrote; other connections' writes wait for the tick.

In a room without ticks, everything room code changes goes out in one state update once the code yields. Each connection's owner writes are applied through `writes` as they arrive.

Work timed by the clock rather than the tick runs in every room: a player's presence grace ending, with `onPlayerLeave`, `onAlarm`, voice mutes expiring, adding bots up to a fill level, and sleep. In a room with ticks, what it changes goes out with the next tick.

## Examples

### A small arena

```ts
// arena.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const Ship = entity("ship", { owner: "string", x: "f32", y: "f32" }, { key: "owner" });

export default defineRoom({
  schema: defineSchema({ entities: [Ship] }),
  tickRate: 20,
  onJoin(room, connection) {
    if (!room.entities.get(Ship, connection.player)) room.entities.create(Ship, { owner: connection.player });
  },
  onTick(room, dt) {
    for (const ship of room.entities.all(Ship)) ship.x = (ship.x + 4 * dt) % 100;
  },
  onPlayerLeave(room, player) {
    const ship = room.entities.get(Ship, player);
    if (ship) room.entities.delete(ship);
  },
});
```

## See also

- [`defineRoom()`](/docs/room/defineroom)
- [`Room`](/docs/room/room)
- [Room SDK](/docs/room/sdk#defineroom)
