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

Property

Modifiers

Type

Description

host

readonly

string

Returns the host url of the server

interpolator

readonly

Interpolator

Returns the instance that is used to interpolate the players' states

isGameLoop

readonly

boolean

isHost

readonly

boolean

Returns true if the client is the host of the game room, the host is the player that created the room

lastError

readonly

string

Returns the last error message if any

players

Record<string, any>

ready

readonly

Promise<void>

Returns a promise that resolves when the client is connected to the server, and the initial state is synced

remotePlayerOpts

PlayerAvatarOpts

Use this to configure the options for the remote player avatars

roomId

readonly

string

Returns the room id of the game server

sessionId

readonly

string

Returns the session id of the client that is assigned by the server

state

readonly

S

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

status

readonly

"disconnected" | "connecting" | "connected"

Returns the status of the connection; possible values are: "disconnected", "connecting", "connected"

synced

readonly

boolean

Returns true if the room state is initially synced with the server

tickRate

readonly

number

Returns the tick rate of the server, this is the number of times the server sends a state update per second

Methods

Method

Modifiers

Description

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 })