Prediction: tick property

Available since @irtio/client 5.0BrowsersNode

The tick read-only property of the Prediction interface is the room tick of the last input, which entity has been stepped through: the local player’s own timeline.

Value

A number. It’s 0 before the first input, and again from a reconnect until the first input after it, including the reconnect after the room moves, restarts or wakes.

Description

With sample, tick advances every room tick, in a room that sends no state updates too. It runs about 2 ticks plus half the round trip ahead of RoomClient.serverTick(), since inputs are sent ahead of the room’s clock so that they arrive in time.

A rule the stepper times by its tick parameter, such as a fuse, runs for the predicted player at prediction.tick. Draw such a timer against it, so the player sees it end on the tick their own moves are stepped at. Don’t use room.tick plus pending for it: room.tick stops in a room where nothing changes.

Examples

Showing a bomb’s fuse

A bomb explodes at a tick the stepper checks, so its countdown is drawn on the player’s timeline, falling back to the room’s clock while tick is 0.

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

const Sapper = entity("sapper", { owner: "string", x: "f32", explodeAt: "u32" });
const schema = defineSchema({ entities: [Sapper] });
const stepper = defineStepper<{ walk: number }>({
  step(world, inputs, dt) {
    for (const s of world.all(Sapper)) s.x += (inputs.get(s.owner)?.walk ?? 0) * 3 * dt;
  },
});

const room = await connect("minefield", { schema });
const me = room.predict(stepper, {
  find: (r) => [...r.entities.all(Sapper)].find((s) => s.owner === r.player),
  sample: () => ({ walk: 1 }),
});

function fuseSeconds(): number | undefined {
  if (!me.entity) return undefined;
  const now = me.tick || Math.floor(room.serverTick());
  return (me.entity.explodeAt - now) / room.tickRate;
}

See also

Runtime availability

BrowsersNode
tick5.05.0