Connection: reckonTick property

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

The reckonTick read-only property of the Connection interface is the time, in ticks to 1/256 of a tick, at which the player’s client last drew the entities it reckons with prediction.reckon(), when it sent the input applied this tick, or the last input applied. Rewind a reckoned kind to it with room.rewind() to check a hit against what the player saw.

Value

A number, with a fraction, from the tick before the input’s tick up to that tick. Don’t use it as a map key or compare it with ===.

  • When the connection opens, it’s the room’s tick then.
  • Each input carries how far past the tick before its own the client last drew reckoned entities, in 256ths of a tick, and 0 when it reckons none. When the room applies the input for tick T, reckonTick becomes T - 1 plus that.

Description

Reckoned entities are drawn on the player’s own timeline, where the room will have them when the input arrives, not in the past as interpolated ones are. So viewTick is the wrong time for them. reckonTick usually falls after the newest tick the room has recorded, so room.rewind() interpolates between that tick and the entities as they are now. Rewind in onTick(), after the stepper has moved them, not in the stepper.

Without a history of the reckoned kind, checking against the present is close: it’s what the player saw to within a tick of the target’s movement.

Examples

Returning a ball where the batter saw it

The batter’s client draws the ball with prediction.reckon(), so a swing is checked against the ball rewound to the batter’s reckon tick.

// batting.ts
import { defineHistory, defineRoom, defineSchema, defineStepper, entity, type RoomEntity } from "@irtio/room";

const Ball = entity("ball", { x: "f32", vx: "f32" });
const flight = defineHistory(Ball, ["x"]);
type Swing = { swing?: boolean };

const stepper = defineStepper<Swing>({
  step(world, _inputs, dt) {
    for (const ball of world.all(Ball)) ball.x += ball.vx * dt;
  },
});

export default defineRoom({
  tickRate: 20,
  schema: defineSchema({ entities: [Ball] }),
  stepper,
  history: [flight],
  onCreate({ room }) {
    room.entities.create(Ball, { x: 20, vx: -15 });
  },
  onTick({ room }) {
    for (const connection of room.connections.values()) {
      if (!(connection.input as Swing | undefined)?.swing) continue;
      for (const seen of room.rewind(flight, connection.reckonTick).values()) {
        const ball = room.entities.get(seen.id) as RoomEntity<typeof Ball> | undefined;
        // The bat reaches 1 unit either side of the batter at x = 0
        if (ball && Math.abs(seen.x) <= 1) ball.vx = Math.abs(ball.vx);
      }
    }
  },
});

The ball is returned from where the room has it now, so only the check uses the rewound position.

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
reckonTick5.05.05.05.0