Multiplayer
Conceptβ
- The Multiplayer module synchronizes a real-time game between players in the same GamePush channel.
- A channel acts as a room. All players connected with the same
channelIdjoin the same multiplayer session. - A player must be a channel member before connecting to multiplayer.
- The SDK automatically selects the host and migrates the role when the current host disconnects.
- Player state, shared world state, and one-time game events are synchronized separately.
- The SDK buffers and interpolates state updates automatically.
- Two synchronization modes are available:
fastfor lower latency andsmoothfor more stable movement.
ποΈ Connection and Room Lifecycle
Multiplayer uses a GamePush channel as a room. All players connecting with the same channelId join the same multiplayer session.
ποΈ Player State
Player state contains data controlled by an individual player. Each player sends their own updates to the host, and the host distributes the resulting state map to the room.
ποΈ Global State
Global state contains shared game entities and values controlled only by the host:
ποΈ Multiplayer Messages
Messages represent one-time actions that do not need to be stored and interpolated as state.
ποΈ Multiplayer in Construct 3
The GamePush plugin for Construct 3 includes an auto-sync engine built on top of GamePush multiplayer. You register the objects you want to synchronize once, start auto-sync β and the plugin does everything else:
Host and Peersβ
Multiplayer uses a host/peer model:
- Host receives player updates, keeps the authoritative state, and sends snapshots to the other players.
- Peers send their own player state to the host and receive the synchronized state of the room.
SDK selects the host automatically and migrates the role if the current host disconnects or another player becomes a better candidate.
Host selection considers connection stability, time spent in the room, and ping. Do not assign a permanent host in the game code. Check gp.multiplayer.isHost and react to the becameHost and becamePeer events instead.
Host selection happens after connecting and may take a few seconds. Do not assume that gp.multiplayer.isHost has its final value immediately after connect() resolves.
Choose the Right Synchronization Mechanismβ
Multiplayer provides three mechanisms for different kinds of data:
| Mechanism | Use it for | Examples |
|---|---|---|
| Player state | State controlled by an individual player | Position, direction, animation, health |
| Global state | Shared entities and rules controlled only by the host | Enemies, items, round timer, shared score |
| Messages | One-time actions that do not need to be stored as state | Shot, explosion, emote, button press |
Do not send continuously changing coordinates as messages. Use player or global state so the SDK can buffer and interpolate them.
Synchronization Modesβ
The mode configures the tick rate and interpolation buffer for both player and global state.
Fastβ
- JavaScript
- Unity
gp.multiplayer.setMode('fast');
GP_Multiplayer.setMode(MultiplayerMode.FAST);
- 60 ticks per second;
- 50β100 ms interpolation buffer;
- lower latency and higher network traffic;
- suitable for action games, shooters, and racing games.
Smoothβ
- JavaScript
- Unity
gp.multiplayer.setMode('smooth');
GP_Multiplayer.setMode(MultiplayerMode.SMOOTH);
- 20 ticks per second;
- 100β300 ms interpolation buffer;
- smoother behavior on unstable connections and lower network traffic;
- suitable for strategies, board games, and slower real-time games.
The default SDK configuration corresponds to smooth.
Choose the mode before starting the game loop. The mode can be changed while connected, but doing so restarts the SDK tick and state-sending loops.
State Schemasβ
Player and global state use schemas with the same field options:
{
x: { interpolate: true },
hp: { interpolate: false },
color: { readonly: true },
}
interpolate: truesmoothly transitions numeric values between received snapshots.interpolate: falseapplies the latest value without interpolation.readonly: truemarks initial metadata that should not be changed by subsequent state updates.
Schemas can contain nested objects and arrays of objects:
- JavaScript
- Unity
gp.multiplayer.defineGlobalSchema({
enemies: {
id: { readonly: true },
x: { interpolate: true },
y: { interpolate: true },
hp: { interpolate: false },
},
});
GP_Multiplayer.defineGlobalSchema(new GP_Data(@"{
""enemies"": {
""id"": { ""readonly"": true },
""x"": { ""interpolate"": true },
""y"": { ""interpolate"": true },
""hp"": { ""interpolate"": false }
}
}"));
The SDK maintains a jitter buffer and automatically interpolates or briefly extrapolates synchronized numeric fields. Game code reads the resulting state without implementing its own network interpolation.
Game Logic and Renderingβ
Use onTick() for game logic and requestAnimationFrame() for rendering:
- JavaScript
- Unity
function gameTick(delta) {
updateGame(delta);
}
function render() {
drawGame();
requestAnimationFrame(render);
}
gp.multiplayer.onTick(gameTick);
requestAnimationFrame(render);
void GameTick(float delta)
{
UpdateGame(delta);
}
void OnEnable()
{
GP_Multiplayer.onTick(GameTick);
}
void OnDisable()
{
GP_Multiplayer.offTick(GameTick);
}
void Update()
{
DrawGame();
}
The SDK tick loop continues working when a browser tab is in the background. Rendering can remain tied to requestAnimationFrame().
Getting Startedβ
- Create or choose a channel for the game room.
- Add all players to the channel.
- Subscribe to multiplayer events and connect using the channel ID.
- Define player and global state schemas.
- Choose a synchronization mode and start the game loop.
Go to the detailed multiplayer documentation π
ποΈ Connection and Room Lifecycle
Multiplayer uses a GamePush channel as a room. All players connecting with the same channelId join the same multiplayer session.
ποΈ Player State
Player state contains data controlled by an individual player. Each player sends their own updates to the host, and the host distributes the resulting state map to the room.
ποΈ Global State
Global state contains shared game entities and values controlled only by the host:
ποΈ Multiplayer Messages
Messages represent one-time actions that do not need to be stored and interpolated as state.
ποΈ Multiplayer in Construct 3
The GamePush plugin for Construct 3 includes an auto-sync engine built on top of GamePush multiplayer. You register the objects you want to synchronize once, start auto-sync β and the plugin does everything else:
Stay in Touchβ
Other documents of this chapter are available here. To get started with GamePush, see the Tutorials chapter.
GamePush Community Telegram: @gs_community.
For your suggestions e-mail: official@gamepush.com
We Wish you Success!