Connection: viewTick property
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe viewTick read-only property of the Connection interface is the tick of the room state the player was looking at when it sent the input applied this tick, or the last input applied. Rewind to it with room.rewind() to check a hit against what the player saw.
Value
A number, a room tick at or before room.tick.
- When the connection opens, it’s the room’s tick then.
- Each input carries the tick of the state its player was looking at, behind the input’s own tick by the round trip and the client’s interpolation delay. When the room applies an input,
viewTickbecomes that tick. - It’s never more than
MAX_REWIND_MS(250 ms) of ticks behind the room. The client reports it, so the room clamps a view tick older than that.
Description
viewTick matters only in a room with a stepper, since inputs go to the stepper. Read it in onTick, which runs right after the stepper, together with input.
Examples
Tagging a runner where the chaser saw it
// tag.ts
import { defineHistory, defineRoom, defineSchema, defineStepper, entity } from "@irtio/room";
const Runner = entity("runner", { owner: "string", x: "f32", y: "f32", it: "bool" }, { key: "owner" });
const trail = defineHistory(Runner, ["x", "y"]);
type Input = { dx: number; dy: number; tag?: string };
const stepper = defineStepper<Input>({
step(world, inputs, dt) {
for (const runner of world.all(Runner)) {
const input = inputs.get(runner.owner);
if (input) {
runner.x += input.dx * 6 * dt;
runner.y += input.dy * 6 * dt;
}
}
},
});
export default defineRoom({
tickRate: 20,
schema: defineSchema({ entities: [Runner] }),
stepper,
history: [trail],
onJoin(room, connection) {
if (!room.entities.get(Runner, connection.player)) room.entities.create(Runner, { owner: connection.player });
},
onTick(room) {
for (const connection of room.connections.values()) {
const tag = (connection.input as Input | undefined)?.tag;
const chaser = room.entities.get(Runner, connection.player);
const target = tag === undefined ? undefined : room.entities.get(Runner, tag);
if (!chaser?.it || !target) continue;
const seen = room.rewind(trail, connection.viewTick).get(target.id);
if (seen && Math.hypot(seen.x - chaser.x, seen.y - chaser.y) < 1) {
chaser.it = false;
target.it = true;
}
}
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
viewTick | 0.1 | 0.1 | 0.1 | 0.1 |