Gameplay helpers
Small building blocks on top of messaging for common multiplayer needs. Everything here is room-scoped and counts against the 30 messages per second budget.
Position sync
{ "action": "updatePosition", "data": { "x": 1.5, "y": 0, "z": -3, "rotation": 90, "animState": "run" } }
x,y,zare required numbers;rotationandanimStateare optional.- Everyone else in the room receives
positionUpdate { playerId, x, y, z, rotation?, animState? }. You do not get your own. - Riff stores your last position with your connection.
- Send at a modest rate (10 per second is plenty). The SDK's
RemotePlayerInterpolatorsmooths the gaps on the receiving side.
await client.UpdatePositionAsync(transform.position, transform.eulerAngles.y, "run");
client.OnPositionUpdate.AddListener(p => /* move the remote player p.PlayerId */);
Raise an event (raiseTurnEvent)
Like sendToRoom but with a short event code and optional targets, for game-rule events.
{ "action": "raiseTurnEvent", "data": { "eventCode": "card.played", "data": { "card": 12 }, "targetPlayerIds": ["p-2"] } }
- No
targetPlayerIds: everyone else in the room receives it. - With
targetPlayerIds: only those players receive it (unknown ids givePLAYER_NOT_FOUNDif none match). - Arrives as event
eventRaised { eventCode, data, fromPlayerId, fromPlayerName }.
eventRaised is not one of the events the Unity SDK handles itself, so it comes through OnMessageReceived with msg.Event == "eventRaised".
Turns
The master decides whose turn it is.
setTurn { targetPlayerId }(master only) → everyone, including the master, receivesturnChanged { targetPlayerId }.- Riff does not enforce turns: it announces them. Your game decides what an out-of-turn action means.
await client.SetTurnAsync(nextPlayerId); // master
client.OnTurnChanged.AddListener(id => myTurn = id == client.LocalPlayerId);
Ending a game
endGame { results } (master only) → everyone, including the master, receives gameEnded { results }. results is any JSON (scores, winner). The room stays open: you can start another round by setting properties and playing on, or leave.
await client.EndGameAsync(new { winner = "p-1", scores });
client.OnGameEnded.AddListener(results => ShowScoreboard(results));
Choosing the right tool
| You want to... | Use |
|---|---|
| Sync a moving player | updatePosition |
| Tell everyone something happened | sendToRoom |
| Ask the master to decide | sendToMaster, master answers with sendToRoom |
| Keep state for late joiners | room / player properties |
| Same random numbers on every client | random seed |
| Announce whose turn it is / the result | setTurn / endGame |