RoomDefinition: writes hook
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe writes hook of a 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
defineRoom({
schema,
writes: {
kindName(room, connection, entity, write) {},
},
});
Parameters
room- The
Room. connection- The
Connectionof the owner that wrote. entity- The 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(), the call’s handler andonLeave()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
// 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
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
writes | 0.1 | 0.1 | 0.1 | 0.1 |