RoomDefinition: onTick() hook
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe onTick() hook of a room definition runs each tick, after the stepper. Only a room with a tickRate has ticks.
Syntax
defineRoom({
tickRate: 20,
onTick(room, dt) {},
});
Parameters
room- The
Room. dt- The fixed timestep in seconds,
1 / tickRate.
Return value
None, or a Promise, which the room doesn’t wait for.
Exceptions
If onTick throws, or its promise rejects, the room stops and its connections close with close code 4007.
defineRoom() throws an Error for a definition with onTick and no tickRate, and the types reject it too.
Description
Each tick applies owner writes through writes, runs the stepper with this tick’s inputs, then runs onTick, then sends the state update to every connection. What onTick changes goes out in that tick’s update.
Inside onTick:
room.tickis the number of the tick running, 1 for a new room’s first.- Each connection’s
inputis the input applied this tick, soonTickcan act on a shot with theviewTickit was aimed at.
Ticks run with a fixed timestep. After a stall a room runs at most 4 ticks to catch up and drops the rest. Ticks stop while a room is hibernated, and none run on wake to catch up; time spent hibernated is in onWake()’s elapsed. Use room.now() or an alarm, not a count of ticks, for anything timed in real time across hibernation.
Examples
Rising lava
// lava.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";
const Level = entity("level", { lava: "f32" });
export default defineRoom({
schema: defineSchema({ state: Level }),
tickRate: 20,
onTick(room, dt) {
room.state.lava = Math.min(room.state.lava + 0.5 * dt, 40);
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
onTick() | 0.1 | 0.1 | 0.1 | 0.1 |