defineStepper() function

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

The defineStepper() function declares a stepper: a simulation step the room runs each tick and its clients run to predict, in a module both import.

Syntax

defineStepper<I>(stepper)

Parameters

stepper

An object with these functions, where I is the type of an input:

step(world, inputs, dt, tick)
Runs one tick. world holds the entities, inputs maps player IDs to their input for this tick, dt is 1 / tickRate seconds, and tick is the tick being stepped.
parseInput(raw) Optional
Runs on the room only, on an input decoded from a client; returns the input, or throws to drop it. Without it, the room passes the input as decoded.
save(world) Optional
Returns the stepper’s own caches for the room, as bytes or JSON, kept in room state. Runs on hibernate and at checkpoints, never per tick.
load(saved, world) Optional
Gets back what save returned, on wake before the room’s first step.
release(world) Optional
Frees what the stepper keeps for a world that’s gone.

Return value

stepper, unchanged, typed as a Stepper<I>.

Description

A room with a stepper needs a tickRate. One stepper serves every room in a room host, so a stepper that keeps caches keys them by world. Bots’ inputs reach step under their player IDs, without parseInput.

Examples

A shared movement step

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

const Glider = entity("glider", { owner: "string", x: "f32", vx: "f32" });
type Thrust = -1 | 0 | 1;

export const stepper = defineStepper<Thrust>({
  parseInput(raw) {
    if (raw !== -1 && raw !== 0 && raw !== 1) throw new TypeError("thrust is -1, 0 or 1");
    return raw;
  },
  step(world, inputs, dt) {
    for (const g of world.all(Glider)) {
      g.vx = g.vx * 0.98 + (inputs.get(g.owner) ?? 0) * 4 * dt;
      g.x += g.vx * dt;
    }
  },
});

export default defineRoom({
  tickRate: 30,
  schema: defineSchema({ entities: [Glider] }),
  stepper,
  onJoin(room, connection) {
    room.entities.create(Glider, { owner: connection.player });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
defineStepper()0.10.10.10.1