# RoomDefinition: writes hook

> The writes hook of a room definition is an object with a handler for each kind with writable fields, which decides what an owner write applies.

The **`writes`** hook of a [room definition](/docs/room/room-definition) is an object with a handler for each kind with writable fields, keyed by kind name. A handler decides what a player's write to an entity it owns applies: the write as sent, values the room changed, or nothing.

## Syntax

```ts
defineRoom({
  schema,
  writes: {
    kindName(room, connection, entity, write) {},
  },
});
```

### Parameters

`room`
: The [`Room`](/docs/room/room).

`connection`
: The [`Connection`](/docs/room/connection) of the owner that wrote.

`entity`
: The [entity](/docs/room/room-entity) written, typed as that kind's entity, before the write applies.

`write`
: An object of the fields written, typed as the kind's writable fields.

### Return value

The fields to apply: `write` to apply it as sent, or other values, such as `write` clamped to the board, for any field of the kind. `undefined` rejects the write, and the owner is corrected.

### Exceptions

If a handler throws, the error is logged, the write is rejected and the owner corrected.

`defineRoom()` throws an `Error` for a key that isn't the private kind or an entity kind of the schema, or names a kind with no writable fields. The types reject those keys too.

## Description

Keys are the names of the private kind and of the entity kinds with writable fields. A kind without a handler applies writes as sent, so a writable field is then a claim the room accepts from the player.

Before a handler runs, the room drops writes to entities the player doesn't own, to fields that aren't writable, and values of the wrong type. A handler must be synchronous. Bots' entities don't go through it: the room writes them directly.

When it runs:

- In a room with ticks, at the start of the tick, before the stepper.
- Earlier, when the writer's connection sends a message or call, or closes, after the write: its writes are applied first, so [`onMessage()`](/docs/room/room-definition/onmessage), the call's handler and [`onLeave()`](/docs/room/room-definition/onleave) see them. Other players' writes still wait for the tick.
- In a room without ticks, as each write arrives, and the state update that follows sends the result.
- Before a room goes to sleep, for the writes it holds.

## Examples

### Keeping a paddle on the court

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

const Paddle = entity("paddle", { y: "f32", frozen: "bool" }, { writable: ["y"] });

export default defineRoom({
  schema: defineSchema({ entities: [Paddle] }),
  tickRate: 30,
  writes: {
    paddle(_room, _connection, paddle, write) {
      if (paddle.frozen) return undefined;
      if (write.y === undefined) return write;
      return { y: Math.min(Math.max(write.y, 0), 480) };
    },
  },
});
```

## See also

- [Ownership](/docs/room/guides/ownership)
- [`RoomEntity`](/docs/room/room-entity)
