For the complete documentation index, see llms.txt. This page is also available as Markdown.

Room-to-Room Data and RPCs

Tutorial 10 - Cluster Properties and Room to Room RPCs

Summary

This tutorial introduces cluster properties and uses them to share data between rooms. Messaging between rooms will be demonstrated using cluster RPCs. This tutorial extends Tutorial 9. Cluster properties are key-value pairs that can be accessed by any room connected to the same cluster. You can use cluster properties to store daily or weekly high score data. Cluster RPCs are like client to server or server to client RPCs, except they are sent between rooms connected to the same cluster. User cluster RPCs for one-time messages between rooms, such as sending information about a player that will be moving from one room to another.

Requirements

Setup

  1. Start with the 'ClusterTutorial' scene from Tutorial 9.

Scripting

Modify the Consts Script to Add More RPCs and a Property

The consts script needs one more RPC id defined: SCORE.

  1. Open and edit Consts.cs from Tutorial 9.

Consts.cs

// RPC IDs
public class RPC
{
    public const uint NUM_ROOMS = 0;
    public const uint JOIN_ROOM = 1;
    public const uint SHUT_DOWN = 2;
    public const uint SCORE = 3;
}

Modify the Server Game Room Script to Generate Scores

We're going to modify the server game room script to generate a score when it receives an RPC from a player. It will read high score data from cluster properties. It will broadcast new high scores to all rooms using a room-to-room cluster RPC.

Cluster properties can be accessed by any room connected to the same cluster. They are transient and will be lost when the cluster is shutdown. Persistent cluster properties is an experimental feature that is disabled. Contact us if you want to test using the experimental cluster properties feature.

  1. Open 'ServerGameRoom.cs' from Tutorial 9.

  2. Define a ksRandom field.

  3. Define an int high score field.

  4. Define an OnGetScoreProperty function.

  5. In the Initialize function, fetch the "highScore" property using Cluster.GetProperty() and assign the OnGetScoreProperty function as the OnComplete callback.

    • Cluster properties are a key-value store where the keys are strings and values are ksMultiTypes. Properties can be grouped as subproperties by using a "." in the property key. When fetching a property, all subproperties of the property are returned. Eg. if you create properties "A", "A.B", "A.C", and "A.D.E", when you fetch property "A", you'll retrieve properties "A", "A.B", "A.C", and "A.D.E". It is possible for a property with no value to have subproperties (in the above example, there is no property "A.D" but there is an "A.D.E"). Our script fetches two properties grouped together as subproperties of "highScore": "highScore.score" and "highScore.username".

  6. Define an OnScore function tagged with [ksRPC(RPC.SCORE)] to generate a random score when a client sends a score RPC. If the score is a new high score, inform all rooms about the new high score using a room-to-room cluster RPC.

    • Similar to client-to-server and server-to-client RPCs, rooms can call RPCs on other rooms using cluster RPCs by calling Cluster.CallRoomRPC(). There are four overloads of this function for specifying which rooms will receive the RPC. One tags an IEnumerable of uint room ids to send the RPC to, one takes a string tag that will send the RPC to all rooms with that tag, one that takes an IEnumerable of string tags that will send the RPC to all rooms that have all the tags, and one that takes no tag or room id arguments that sends it to all rooms in the cluster (including the caller). Instead of [ksRPC(id)], cluster RPC handler functions are tagged with [ksClusterRPC(id)]. Cluster RPC handlers tagged with [ksClusterRPC(id, false)] are called off the main-thread.

  7. Define an OnHighScore function tagged with [ksClusterRPC(RPC.SCORE)] to log new high scores that are received via room-to-room cluster RPCs.

ServerGameRoom.cs

Modify the Server Lobby Room Script to Handle High Scores

We're going to modify the server lobby room script to read and write high score data to cluster properties.

  1. Open 'ServerLobbyRoom.cs' from Tutorial 9.

  2. Define an OnGetScoreProperty function.

  3. In the Initialize function, fetch the "highScore.score" property from cluster properties using Cluster.GetProperty() and assign the OnGetScoreProperty function as the OnComplete callback.

  4. Define an OnHighScore function tagged with [ksClusterRPC(RPC.SCORE)] to log new high scores that are received via room-to-room cluster RPCs and write them to cluster properties.

ServerLobbyRoom.cs

Modify the Client Game Room Script to Request a Score When Space is Pressed

We're going to make the client game room script call the score RPC on the server when space is pressed.

  1. Open 'ClientGameRoom.cs' from Tutorial 9.

  2. Call the SCORE RPC with your username when space is pressed in the Update function.

ClientGameRoom.cs

Testing

  1. Start a local cluster running the 'ClusterTutorial' scene and 'LobbyRoom' room type.

  2. Check the 'Use Local Server' checkbox in the inspector for the 'Connect' script.

  3. Enter play mode.

  4. Press J to enter a game room.

    • You may see a message saying there are no rooms to join if the lobby hasn't started a game room yet. Wait a bit and try again.

When you press space while in a game room, the server will log a random score for you. If it's a new high score, the high score and your name will appear in the logs for all running rooms. When you start a new game room, the game room's logs will log the current high score and the username of the player who set the score.

Last updated