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 Parameter | Default type |
|---|---|
View | unknown |
Command | unknown |
Constructors
Constructor
new Predictor<View, Command>(room, options): Predictor<View, Command>;Defined in: client/predict.ts:80
Parameters
| Parameter | Type |
|---|---|
room | RoomClient<View, Command> |
options | PredictorOptions<View, Command> |
Returns
Predictor<View, Command>
Accessors
state
Get Signature
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
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()
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
| Parameter | Type |
|---|---|
command | Command |
Returns
Promise<void>
close()
close(): void;Defined in: client/predict.ts:176
Stop following the room. The room itself stays connected.
Returns
void