Multiplayer Messages
Messages represent one-time actions that do not need to be stored and interpolated as state.
Method Listβ
Actions:
gp.multiplayer.sendMessage()- send a one-time event to other players. FREE
Events:
gp.multiplayer.onMessage()- subscribe to multiplayer messages. FREEgp.multiplayer.offMessage()- unsubscribe from multiplayer messages. FREE
Use messages for events such as:
- a player firing a weapon;
- an explosion or visual effect;
- an emote;
- starting or restarting a round;
- interaction with a map object.
Use player state or global state when late snapshots must still contain the value.
Receive Messagesβ
Register one handler and route messages by eventName:
- JavaScript
- Unity
function onMultiplayerMessage({
eventName,
senderId,
data,
timestamp,
}) {
if (eventName === 'shoot') {
createBullet({
ownerId: senderId,
x: data.x,
y: data.y,
direction: data.direction,
});
}
if (eventName === 'emote') {
showEmote(senderId, data.name);
}
}
gp.multiplayer.onMessage(onMultiplayerMessage);
void OnMultiplayerMessage(GP_Data message)
{
Debug.Log(message.Data);
}
GP_Multiplayer.onMessage(OnMultiplayerMessage);
The handler receives:
| Field | Description |
|---|---|
eventName | Message type defined by the game |
senderId | ID of the player who sent the message |
data | Arbitrary JSON-compatible payload |
timestamp | Message creation time |
Send to Other Playersβ
With no target, the message is sent to all other players:
- JavaScript
- Unity
gp.multiplayer.sendMessage('shoot', {
x: player.x,
y: player.y,
direction: player.direction,
});
GP_Multiplayer.sendMessage(
"shoot",
new GP_Data(@"{
""x"": 100,
""y"": 200,
""direction"": 1
}")
);
'all' can be passed explicitly:
- JavaScript
- Unity
gp.multiplayer.sendMessage(
'roundStarted',
{ round: 2 },
'all',
);
GP_Multiplayer.sendMessage(
"roundStarted",
new GP_Data(@"{ ""round"": 2 }"),
"all"
);
To send a message to one player, pass their playerId:
- JavaScript
- Unity
gp.multiplayer.sendMessage(
'privateEmote',
{ name: 'wave' },
targetPlayerId,
);
GP_Multiplayer.sendMessage(
"privateEmote",
new GP_Data(@"{ ""name"": ""wave"" }"),
targetPlayerId
);
Process a Message Locally with Echoβ
By default, the sender does not receive their own message. Use echo: true to run the same local handler immediately:
- JavaScript
- Unity
gp.multiplayer.sendMessage(
'explosion',
{
x: 320,
y: 180,
radius: 80,
},
{
target: 'all',
echo: true,
},
);
GP_Multiplayer.sendMessage(
"explosion",
new GP_Data(@"{
""x"": 320,
""y"": 180,
""radius"": 80
}"),
new MultiplayerSendMessageOptions
{
target = "all",
echo = true
}
);
The sender receives the echoed event locally without waiting for a network round trip. Other players receive it through multiplayer normally.
This is useful when all clients, including the sender, should execute the same event handler.
Choose an Authority Modelβ
Messages transport an event but do not validate game rules. Decide which client is allowed to produce or confirm an event.
For host-authoritative actions, peers can send a request and the host can broadcast the accepted result:
- JavaScript
- Unity
function onMultiplayerMessage({ eventName, senderId, data }) {
if (eventName === 'requestOpenChest' && gp.multiplayer.isHost) {
if (canOpenChest(senderId, data.chestId)) {
openChestInGlobalState(data.chestId);
gp.multiplayer.sendMessage('chestOpened', {
chestId: data.chestId,
playerId: senderId,
});
}
}
}
gp.multiplayer.sendMessage('requestOpenChest', {
chestId: 'chest-7',
});
void RequestOpenChest(string chestId)
{
GP_Multiplayer.sendMessage(
"requestOpenChest",
new GP_Data($@"{{ ""chestId"": ""{chestId}"" }}")
);
}
// On the host, validate the message received through onMessage(),
// update globalState, and send the accepted result to all players.
The durable result belongs in globalState; the messages represent the request and notification.
Unsubscribeβ
Pass the same function reference used during subscription:
- JavaScript
- Unity
gp.multiplayer.offMessage(onMultiplayerMessage);
GP_Multiplayer.offMessage(OnMultiplayerMessage);
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!