Prediction: mispredict event

Available since @irtio/client 5.0BrowsersNode

The mispredict event of the Prediction interface fires after each replay that found the room’s state different from the prediction in a field the stepper writes.

Syntax

addEventListener("mispredict", (event) => {})

Event type

A CustomEvent. Its detail is an object with these properties:

tick
A number: the room tick of the state the prediction replayed from.
fields
An array of strings: the fields of the predicted entity that differed.
delta
An object with a number for each numeric field that differed: the prediction’s value less the replayed one, which render() smooths away. An angle field’s delta is taken the short way round.

Description

The event fires after the replay, once mispredictions has counted it. A field no step writes never appears in fields, and a quat field appears in fields with no delta.

Examples

Logging mispredictions while tuning a stepper

While a platformer’s jump is tuned, each misprediction is logged with how far off it was.

// tune-jump.ts
import type { Prediction } from "@irtio/client";

export function logMispredictions(me: Prediction<unknown>, signal: AbortSignal): void {
  me.addEventListener(
    "mispredict",
    ({ detail: { tick, fields, delta } }) => {
      const sizes = fields.map((f) => `${f} ${delta[f]?.toFixed(3) ?? "changed"}`);
      console.warn(`mispredicted at tick ${tick}: ${sizes.join(", ")}`);
    },
    { signal },
  );
}

Aborting signal removes the listener once tuning is done.

See also

Runtime availability

BrowsersNode
mispredict event5.05.0