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

Cluster Sample

Summary

This sample creates a lobby which manages game instances. Players connect to the lobby and are sent to an available game instance. The game selects a group of winners and then spawns a new game instance for those players. Other players are sent back to the lobby.

Requirements

Define consts

In 'ReactorScripts/Common', create a Consts.cs file to hold const values we will use on the client and server. The consts are ids for properties and RPCs. Add the following to your script: Consts.cs

public class Prop
{
    public const uint NAME = 0;
}

public class RPC
{
    public const uint TOKEN = 0;
    public const uint GAME_OVER = 1;
    public const uint MOVE_TO_ROOM = 2;
    public const uint RETURN_TO_LOBBY = 3;
    public const uint PLAYER_DATA = 4;
    public const uint ACKNOWLEDGE_PLAYER_DATA = 5;
}

Create the lobby scene

  1. Create a new scene and name it 'Lobby'.

  2. Create a game object named 'Lobby Room' and add a ksRoomType.

  3. Save the scene.

  4. Add the scene to the build settings with 'File->Build Settings->Add Open Scenes'.

Create the game scene

  1. Save your scene and create a new scene named 'Game'.

  2. Create a game object named 'Game Room' and add a ksRoomType.

  3. Save the scene.

  4. Add the scene to the build settings with 'File->Build Settings->Add Open Scenes'.

Create a room launcher script

The room launcher script will be used to launch rooms and send players to those rooms when they are ready.

  1. Open the 'Lobby' scene.

  2. Add a new server room script to the 'Lobby Room' game object and name it 'sRoomLauncher'.

sRoomLauncher.cs

Create a lobby script

The lobby starts a game room and sends players to it once there are at least 10 players or after 30 seconds.

  1. Add a new server room script to the 'Lobby Room' game object and name it 'sLobby'.

sLobby.cs

Create a connect script

The connect script has a static ksRoomInfo field it will connect to from Start if it is set, otherwise it connects to a local or online server depending on how it is configured.

  1. Add a new Monobehaviour to the 'Lobby Room' game object and name it 'Connect' Connect.cs

Create a client room script

The client room responds to server RPCs to switch scenes and connect to new rooms.

  1. Add a new client room script to the 'Lobby Room' game object and name it 'cRoom'.

  2. Save the scene.

cRoom.cs

Add scripts to the game room

  1. Open the 'Game' scene.

  2. Add a 'Connect' script to the 'Game Room' game object.

  3. Add a 'cRoom' script to the 'Game Room' game object.

  4. Save the scene.

Run and connect to the local cluster

  1. Load the Lobby scene.

  2. Build configs (CTRL+F2).

  3. Click 'Reactor->Launch Local Cluster' to bring up the local cluster window.

  4. Set the Launch Scene to 'Lobby'. The Launch Room should say 'Lobby Room'. Leave the other settings as the defaults.

  5. Click 'Launch Local Cluster'.

  6. The local cluster should now be running and the local server logs panel will show you the cluster logs. You can use the drop down in the log panel to view logs for the rooms. The first entry after the cluster should be for the lobby. Select it to view the lobby logs.

  7. Make sure 'Use Live Server' is unchecked when inspecting the 'Lobby Room' 'Connect' script.

  8. Enter play mode. You will connect to the lobby.

  9. After 30 seconds, the lobby server logs should say 'Starting room Game'.

  10. Once the game room is started, you will load the Game scene and connect to the game room.

  11. Stop the cluster opening the local cluster window ('Reactor->Launch Local Cluster') and clicking 'Stop Local Cluster'.

Create a game room script

The game room randomly chooses winners and losers. If there are any winners, it launches a new game room and sends the winners to the new room. Losers are sent back to the lobby. The game room shutsdown after the game is finished and players are sent to the next room, or if no players are connected for 60 seconds.

  1. Load the Game scene.

  2. Add an 'sRoomLauncher' to the 'Game Room' game object.

  3. Add a new server room script to the 'Game Room' game object and name it 'sGameRoom'.

sGameRoom.cs

Handle game over and return to lobby RPCs in the client room script

  1. Open 'cRoom.cs'.

  2. Add a OnGameOver function to log whether you win or lose.

  3. Add a OnReturnToLobby function to return to the lobby.

cRoom.cs

Run and connect to the local cluster

  1. Save the scene.

  2. Build configs (CTRL+F2).

  3. Open the 'Lobby' scene.

  4. Launch the local cluster with the same settings as before.

  5. Make sure 'Use Live Server' is unchecked when inspecting the 'Lobby Room' 'Connect' script.

  6. Enter play mode. You will connect to the lobby.

  7. After 30 seconds, the lobby server logs should say 'Starting room Game'.

  8. One the game room is started, you will load the 'Game' scene and connect to the game room.

  9. After 10 seconds in the game room, you will randomly win or lose and see a 'You Win!' or 'You Lose!' message in the Unity logs. If you win, you will reload the scene and connect to a new game room. If you lose, you will load the Lobby scene and reconnect to the lobby.

  10. Stop the local cluster.

Create a player script

The player script has a token field used to identify and authenticate players when they move between rooms.

  1. Add a new server player script to the 'Lobby Room' game object and name it 'sPlayer'.

sPlayer.cs

  1. Save the scene.

  2. Open the 'Game' scene.

  3. Add the 'sPlayer' script to the 'Game Room' game object and save the scene.

Authenticate players and add bots in the lobby script

  1. Open 'sLobby.cs'.

  2. Add a ksRandom field.

  3. Add a HashSet field to store tokens.

  4. Add a GenerateToken function that generates a unique token.

  5. Add PlayerJoin and PlayerLeave functions. The PlayerJoin function assigns a token to the player and adds it to the token set. The PlayerLeave function removes it from the set so it can be reused.

  6. Add an Authenticate fuction that sets the player's name to a value provided by the client.

  7. Modify the Update function to add a virtual player (bot) every 2 seconds.

sLobby.cs

Send a username or token when connecting

  1. Open 'Connect.cs'.

  2. Add an optional token parameter to ConnectToServer. If a token is non-zero, pass it to Room.Connect. Otherwise pass a username.

  3. Pass the token to ConnectToServer in the Start function if the RoomInfo is set.

Connect.cs

Set the token in the client room script

  1. Open 'cRoom.cs'.

  2. Add an OnSetToken function that sets the token.

cRoom.cs

Use cluster RPCs to send player data to new rooms

  1. Open 'sRoomLauncher.cs'.

  2. Add a private ksRoomInfo field.

  3. Change the IsLaunchingRoom property to also check if that the ksRoomInfo is not null.

  4. In the Update function, when the task completes, instead of sending players to the new room, use a cluster RPC to send player data to the new room that it will use to authenticate players, and store the room info.

  5. In the LaunchRoom function, don't launch the room if all the players being sent to the room are bots.

  6. In the SendPlayers function, disconnect virtual players (bots).

  7. Add an OnAcknowledgePlayerData that sends the players to the new room when it acknowledges it received the player data.

sRoomLauncher.cs

Authenticate players in the game room

  1. Open 'sGameRoom.cs'.

  2. Add a private dictionary field m_playerData that maps tokens to usernames.

  3. Add an Authenticate function that sets a player's name by retrieving it from the player data using their token. If they do not provide a valid token, they do not pass authentication. Store their token in their player script.

  4. Log the names of eliminated players in the FinishGame function.

  5. Add an OnGetPlayerData function that fills the player data dictionary with the received data and calls a cluster RPCs to tell the room that started this room it got the player data.

sGameRoom.cs

Run and connect to the local cluster

  1. Open the 'Lobby' scene.

  2. Build the configs (CTRL+F2).

  3. Launch the local cluster with the same settings as before.

  4. Make sure 'Use Live Server' is not checked when inspecting the 'Lobby Room' 'Connect' script.

  5. Enter play mode. You will connect to the lobby.

  6. You should see a message in the server logs for the lobby saying 'MyName connected' (or whatever you changed 'MyName' to). Every 2 seconds you should see a message about a bot connecting.

  7. After 10 players (bots) are connected, the lobby server logs should say 'Starting room Game'.

  8. Once the game room is started, you will load the 'Game' scene and connect to the game room.

  9. After 10 seconds in the game room, you will randomly with or lose and see a 'You Win!' or 'You Lose!' message in the Unity logs. If you win, you will reload the scene and connect to a new game room. If you lose, you will load the 'Lobby' scene and reconnect to the lobby.

  10. The names of all players who lost will be logged in the Game room logs.

  11. Stop the local cluster.

Run and connect to an online cluster

  1. Click 'Reactor->Publishing'.

  2. Give the image a name and version and click 'Publish'.

  3. Click on your project in the 'Reactor Projects' section.

  4. Click the 'Images' tab.

  5. Click 'Launch' in the row for the image and version you just published.

  6. Check 'Launch Cluster'.

  7. Select 'Lobby' for the 'Scene'.

  8. The 'Room' should say 'Lobby Room'

  9. 'Public Instance' should be checked.

  10. Check 'Keep Alive'.

  11. Give your cluster a name.

  12. Click 'Launch'.

  13. In Unity, make sure 'Use Live Server' is checked when inspecting the 'Lobby Room' 'Connect' script.

  14. Enter play mode. You should connect to the live cluster this time. You should load the game scene and connect to the game room in about 30 seconds once enough bots are connected.

Stopping the cluster

  1. When you are done, go to the 'Sessions' tab in the web console.

  2. Expand the cluster to see the rooms running in the cluster.

  3. Click the 'X' in the 'Stop' column for the cluster, then click the checkmark to stop the cluster. It may take several seconds for the cluster to stop.

Last updated