RoomDefinition: onTick() hook

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The 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.tick is the number of the tick running, 1 for a new room’s first.
  • Each connection’s input is the input applied this tick, so onTick can act on a shot with the viewTick it 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 roomsirt dev@irtio/testingSelf-hosted room host
onTick()0.10.10.10.1