GameRoom class

This class is used to communicate with the game server and sync the game state

Signature:

export declare class GameRoom<S extends BaseRoomState, RM = any, CM = any> 

Remarks

The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the GameRoom class.

Properties

PropertyModifiersTypeDescription
hostreadonlystringReturns the host url of the server
interpolatorreadonlyInterpolatorReturns the instance that is used to interpolate the players' states
isHostreadonlybooleanReturns true if the client is the host of the game room, the host is the player that created the room
lastErrorreadonlystringReturns the last error message if any
readyreadonlyPromise<void>Returns a promise that resolves when the client is connected to the server, and the initial state is synced
remotePlayerOptsPlayerAvatarOptsUse this to configure the options for the remote player avatars
roomIdreadonlystringReturns the room id of the game server
sessionIdreadonlystringReturns the session id of the client that is assigned by the server
statereadonlyS

Returns the current state of the room, the game state is regularly updated by the server and synced with the clients

cf BaseRoomState for common properties on the state; the game server can add custom properties to the state that are specific to the game

statusreadonly"disconnected" | "connecting" | "connected"Returns the status of the connection; possible values are: "disconnected", "connecting", "connected"
syncedreadonlybooleanReturns true if the room state is initially synced with the server
tickRatereadonlynumberReturns the tick rate of the server, this is the number of times the server sends a state update per second

Methods

MethodModifiersDescription
getPlayerStateSync(selector)Returns the instance that is used to sync the players' states
leave()Disconnects the client from the server
onConnect(listener)listen to connect events, this is fired when the client is connected to the server, but not necessarily synced
onDisconnect(listener)listen to disconnect events, this is fired when the client is disconnected from the server
onMessage(listener)listen to incoming messages from the server
onPlayerJoined(listener)listen to player joined events, this is fired whenever a new player joins the game room
onPlayerLeft(listener)listen to player left events, this is fired whenever a player leaves the game room
onStart(listener)listen to game start notifications from the server
onState(listener)listen to state events, this is fired whenever the client receives a state update from the server as defined by the tick rate
onStop(listener)listen to game stop notifications from the server
onSync(listener)listen to sync events, this is fired when the client is connected to the server and the initial state is synced
requestStart(countdown)Sends a request to the server to start the game
send(data)

Sends a message to the server, the data type is specific to the game

For example client.send({ type: "hit", damage: 10 })