Reckoner
@irtio/client 5.0BrowsersNodeThe Reckoner interface of the Client API draws remote entities of one kind as the stepper moves them on a prediction world, on the local player’s timeline. Prediction.reckon() returns one.
Instance properties
All are read only.
Reckoner.kindandReckoner.fields- The kind and the fields it draws.
Instance methods
Reckoner.get()- Returns an entity’s fields as the prediction world has them, carried on to the time being drawn.
Description
An Interpolator draws remote entities a little behind the room’s clock, and the local player is predicted a little ahead of it. A reckoner draws entities that move on their own, by rules the stepper knows, such as projectiles, falling crates and trains, at the same time as the local player, so the player dodges a rocket where the room has it when the player’s input arrives.
The prediction world steps every entity the stepper moves, from the room’s last state through the inputs the room hasn’t acknowledged. A rocket that room code turns or stops outside the stepper is drawn on its old path until the room’s state arrives, and then eased onto the new one by 20% a tick, as Prediction.render() eases a misprediction.
kind is the entity kind passed to reckon(), and fields is the array of field names passed with it, the ones get() returns.
Examples
Drawing falling crates
Crates fall by gravity in the stepper, so they’re reckoned rather than interpolated.
// crates.ts
import { connect, defineSchema, defineStepper, entity } from "@irtio/client";
const Porter = entity("porter", { owner: "string", x: "f32" });
const Crate = entity("crate", { x: "f32", y: "f32", vy: "f32" });
const schema = defineSchema({ entities: [Porter, Crate] });
const stepper = defineStepper<{ walk: number }>({
step(world, inputs, dt) {
for (const c of world.all(Crate)) {
c.vy = c.y > 0 ? c.vy - 9.8 * dt : 0;
c.y = Math.max(0, c.y + c.vy * dt);
}
for (const p of world.all(Porter)) p.x += (inputs.get(p.owner)?.walk ?? 0) * 4 * dt;
},
});
const room = await connect("warehouse", { schema });
const me = room.predict(stepper, {
find: (r) => [...r.entities.all(Porter)].find((p) => p.owner === r.player),
sample: () => ({ walk: 0 }),
});
const crates = me.reckon(Crate, ["x", "y"]);
Each frame, crates.get(crate) gives where to draw a crate.
See also
Runtime availability
| Browsers | Node | |
|---|---|---|
Reckoner | 5.0 | 5.0 |