Prediction: drift property

Available since @irtio/client 5.0BrowsersNode

The drift read-only property of the Prediction interface is a summary of the mispredictions of about the last second, so you can tell a stepper that disagrees with the room from a few late inputs.

Value

An object with these properties:

rate
A number from 0 to 1: the share of replays that mispredicted.
average
A number: the size of mispredictions, with matched replays counting 0.
peak
A number: the largest recent misprediction, falling to a tenth within a second.
last
A number: the size of the last misprediction, kept until the next one, and 0 before the first.
fields
A read-only array of strings: the fields of the last misprediction.
verdict

A string, from rate:

"matched"
rate is under 1%. The prediction ran what the room ran.
"jitter"
rate is from 1% to under 50%: occasional mispredictions, which render() smooths, such as an input that reached the room after its tick.
"diverging"
rate is 50% or more, so the stepper and the room disagree: a step that reads something besides world, inputs, dt and tick, room code that moves the predicted entity outside the stepper, or a rule only onTick() applies.

Description

A misprediction’s size is the largest of its numeric fields’ deltas, as the mispredict event reports them, and the angle in radians a quat field was off by. Fields of other types count toward rate with no size.

The averages are kept per room tick with a half-second time constant, whatever the tick rate. Ticks with no state update count as matched once half a second has passed, so the verdict returns to "matched" in a room that goes quiet. snap(), a misprediction render() drops for snapDistance, a reconnect and a new room generation reset it.

Pass warnOnDivergence: true to RoomClient.predict() to log a console warning when the verdict turns "diverging".

Examples

Showing a drift warning while you develop

A development build shows which fields diverge, so the stepper’s author can find the rule the room applies outside it.

// drift-badge.ts
import type { Prediction } from "@irtio/client";

export function driftBadge(me: Prediction<unknown>): string {
  const { verdict, rate, fields } = me.drift;
  if (verdict === "matched") return "";
  return `${verdict}: ${Math.round(rate * 100)}% of replays missed ${fields.join(", ")}`;
}

See also

Runtime availability

BrowsersNode
drift5.05.0