Skip to content

Class: Predictor<View, Command> ​

Defined in: client/predict.ts:63

Shows a player the effect of their own commands at once, instead of when the room's answer arrives.

state is the room's state with this player's unconfirmed commands applied on top, by the apply you give. When the room's state arrives it replaces the guess: commands the room has applied are part of it, commands it refused are dropped, and those still on their way are applied again. So a wrong guess lasts until the room's answer, and never longer.

const predicted = new Predictor(room, {
  apply(state, command, you) {
    if (command.type === "move") state.units[you].target = command.to;
  },
});
button.onclick = () => predicted.send({ type: "move", to });
draw(predicted.state);

Send through predicted.send, and draw from predicted.state where you drew from room.state. Parts of the state the prediction did not change are the same objects as in room.state.

Type Parameters ​

Type ParameterDefault type
Viewunknown
Commandunknown

Constructors ​

Constructor ​

ts
new Predictor<View, Command>(room, options): Predictor<View, Command>;

Defined in: client/predict.ts:80

Parameters ​

ParameterType
roomRoomClient<View, Command>
optionsPredictorOptions<View, Command>

Returns ​

Predictor<View, Command>

Accessors ​

state ​

Get Signature ​

ts
get state(): View | undefined;

Defined in: client/predict.ts:94

The room's state as it will be once the room has applied this player's unconfirmed commands. Undefined until the room's first state arrives.

Returns ​

View | undefined


lead ​

Get Signature ​

ts
get lead(): number;

Defined in: client/predict.ts:143

With tick: how many ticks ahead of the room's state the prediction runs, which is how long a command takes to show there. Measured from the commands sent so far; 1 before the first has come back.

Returns ​

number

Methods ​

send() ​

ts
send(command): Promise<void>;

Defined in: client/predict.ts:152

Send a command, and show its effect at once. Resolves and rejects as RoomClient.send does. If the room refuses the command, its effect is taken back.

Parameters ​

ParameterType
commandCommand

Returns ​

Promise<void>


close() ​

ts
close(): void;

Defined in: client/predict.ts:176

Stop following the room. The room itself stays connected.

Returns ​

void

MIT licence. Tickflare is an independent project and is not affiliated with Cloudflare.