Prediction: stop() method

Available since @irtio/client 5.0BrowsersNode

The stop() method of the Prediction interface stops sampling and pushing inputs and frees what the stepper keeps for the prediction world.

Syntax

stop()

Parameters

None.

Return value

None (undefined).

Exceptions

None.

Description

A client runs one prediction at a time, since inputs are numbered per client: RoomClient.predict() throws while another prediction on the client hasn’t been stopped. To predict with a new stepper or sample, stop the old one first.

stop() calls the stepper’s release(), if it has one, with the prediction world. Calling it again does nothing. After it, push() does nothing: the input isn’t stepped or sent. A stopped prediction stays in RoomClient.predictions, and still reconciles the inputs it sent.

A sample that throws stops the prediction as stop() does.

Examples

Switching from a runner to a car

When the player gets into a car, the game stops predicting the runner and predicts the car with the car’s stepper.

// get-in.ts
import { defineSchema, defineStepper, entity, type Prediction, type RoomClient } from "@irtio/client";

const Car = entity("car", { driver: "string", speed: "f32" });
export const schema = defineSchema({ entities: [Car] });
const carStepper = defineStepper<{ throttle: number }>({
  step(world, inputs, dt) {
    for (const c of world.all(Car)) c.speed += (inputs.get(c.driver)?.throttle ?? 0) * 4 * dt;
  },
});

export function getIn(room: RoomClient, onFoot: Prediction<unknown>, throttle: () => number) {
  onFoot.stop();
  return room.predict(carStepper, {
    find: (r) => [...r.entities.all(Car)].find((c) => c.driver === r.player),
    sample: () => ({ throttle: throttle() }),
  });
}

See also

Runtime availability

BrowsersNode
stop()5.05.0