defineStepper() function
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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
Iis the type of an input:step(world, inputs, dt, tick)- Runs one tick.
worldholds the entities,inputsmaps player IDs to their input for this tick,dtis1 / tickRateseconds, andtickis 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
savereturned, on wake before the room’s first step. release(world)Optional- Frees what the stepper keeps for a
worldthat’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
- The stepper
- Physics and
@irtio/physics, a stepper built on Rapier
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
defineStepper() | 0.1 | 0.1 | 0.1 | 0.1 |