Reckoner: get() method

Available since @irtio/client 5.0BrowsersNode

The get() method of the Reckoner interface returns an entity’s fields as they are after the prediction’s last step, at Prediction.tick, carried on at that step’s velocity for the part of a tick since.

Syntax

get(entity)

Parameters

entity
A client entity of the reckoner’s kind.

Return value

An object with each field the reckoner was made with: a number for a numeric or angle field, and a Quat for a quat field.

Exceptions

None.

Description

Call get() once per entity each frame. It moves smoothly between steps, and by at most a tick’s movement past where a step stops the entity. When a replay moves a field a step wrote, get() adds a share of the jump, which shrinks by 20% a tick, so the entity eases onto its new path.

get() returns the entity’s own values, as the room last sent them:

  • for a field no step wrote
  • for every field before the prediction steps
  • for an entity that entered the client’s view since the prediction’s last step

Right after connecting, and when the round trip grows, the prediction moves its inputs further ahead of the room and skips the ticks between, so reckoned entities fall behind by those ticks’ motion and catch up over the next 20 or so ticks.

Examples

Drawing arrows in flight

Arrows fly by their velocity in the stepper, and the client draws each one where get() puts it, turned to its heading.

// arrows.ts
import { connect, defineSchema, defineStepper, entity } from "@irtio/client";

const Archer = entity("archer", { owner: "string", x: "f32" });
const Arrow = entity("arrow", { x: "f32", y: "f32", heading: "angle", speed: "f32" });
const schema = defineSchema({ entities: [Archer, Arrow] });
const stepper = defineStepper<{ dx: number }>({
  step(world, inputs, dt) {
    for (const a of world.all(Arrow)) {
      a.x += Math.cos(a.heading) * a.speed * dt;
      a.y += Math.sin(a.heading) * a.speed * dt;
    }
    for (const p of world.all(Archer)) p.x += (inputs.get(p.owner)?.dx ?? 0) * 3 * dt;
  },
});

const room = await connect("range", { schema });
const me = room.predict(stepper, {
  find: (r) => [...r.entities.all(Archer)].find((p) => p.owner === r.player),
  sample: () => ({ dx: 0 }),
});
const arrows = me.reckon(Arrow, ["x", "y", "heading"]);

function frame() {
  for (const a of room.entities.all(Arrow)) {
    const { x, y, heading } = arrows.get(a);
    console.log(`arrow ${a.id} at ${x}, ${y} facing ${heading}`);
  }
  requestAnimationFrame(frame);
}
frame();

See also

Runtime availability

BrowsersNode
get()5.05.0