Riff

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, z are required numbers; rotation and animState are 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 RemotePlayerInterpolator smooths 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 give PLAYER_NOT_FOUND if 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, receives turnChanged { 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 playerupdatePosition
Tell everyone something happenedsendToRoom
Ask the master to decidesendToMaster, master answers with sendToRoom
Keep state for late joinersroom / player properties
Same random numbers on every clientrandom seed
Announce whose turn it is / the resultsetTurn / endGame