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
Create a new scene and name it 'Lobby'.
Create a game object named 'Lobby Room' and add a ksRoomType.
Save the scene.
Add the scene to the build settings with 'File->Build Settings->Add Open Scenes'.
Create the game scene
Save your scene and create a new scene named 'Game'.
Create a game object named 'Game Room' and add a ksRoomType.
Save the scene.
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.
Open the 'Lobby' scene.
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.
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.
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.
Add a new client room script to the 'Lobby Room' game object and name it 'cRoom'.
Save the scene.
cRoom.cs
Add scripts to the game room
Open the 'Game' scene.
Add a 'Connect' script to the 'Game Room' game object.
Add a 'cRoom' script to the 'Game Room' game object.
Save the scene.
Run and connect to the local cluster
Load the Lobby scene.
Build configs (CTRL+F2).
Click 'Reactor->Launch Local Cluster' to bring up the local cluster window.
Set the Launch Scene to 'Lobby'. The Launch Room should say 'Lobby Room'. Leave the other settings as the defaults.
Click 'Launch Local Cluster'.
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.
Make sure 'Use Live Server' is unchecked when inspecting the 'Lobby Room' 'Connect' script.
Enter play mode. You will connect to the lobby.
After 30 seconds, the lobby server logs should say 'Starting room Game'.
Once the game room is started, you will load the Game scene and connect to the game room.
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.
Load the Game scene.
Add an 'sRoomLauncher' to the 'Game Room' game object.
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
Open 'cRoom.cs'.
Add a OnGameOver function to log whether you win or lose.
Add a OnReturnToLobby function to return to the lobby.
cRoom.cs
Run and connect to the local cluster
Save the scene.
Build configs (CTRL+F2).
Open the 'Lobby' scene.
Launch the local cluster with the same settings as before.
Make sure 'Use Live Server' is unchecked when inspecting the 'Lobby Room' 'Connect' script.
Enter play mode. You will connect to the lobby.
After 30 seconds, the lobby server logs should say 'Starting room Game'.
One the game room is started, you will load the 'Game' scene and connect to the game room.
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.
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.
Add a new server player script to the 'Lobby Room' game object and name it 'sPlayer'.
sPlayer.cs
Save the scene.
Open the 'Game' scene.
Add the 'sPlayer' script to the 'Game Room' game object and save the scene.
Authenticate players and add bots in the lobby script
Open 'sLobby.cs'.
Add a ksRandom field.
Add a HashSet field to store tokens.
Add a
GenerateTokenfunction that generates a unique token.Add
PlayerJoinandPlayerLeavefunctions. ThePlayerJoinfunction assigns a token to the player and adds it to the token set. ThePlayerLeavefunction removes it from the set so it can be reused.Add an
Authenticatefuction that sets the player's name to a value provided by the client.Modify the
Updatefunction to add a virtual player (bot) every 2 seconds.
sLobby.cs
Send a username or token when connecting
Open 'Connect.cs'.
Add an optional token parameter to
ConnectToServer. If a token is non-zero, pass it toRoom.Connect. Otherwise pass a username.Pass the token to
ConnectToServerin theStartfunction if theRoomInfois set.
Connect.cs
Set the token in the client room script
Open 'cRoom.cs'.
Add an
OnSetTokenfunction that sets the token.
cRoom.cs
Use cluster RPCs to send player data to new rooms
Open 'sRoomLauncher.cs'.
Add a private ksRoomInfo field.
Change the
IsLaunchingRoomproperty to also check if that the ksRoomInfo is not null.In the
Updatefunction, 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.In the
LaunchRoomfunction, don't launch the room if all the players being sent to the room are bots.In the
SendPlayersfunction, disconnect virtual players (bots).Add an
OnAcknowledgePlayerDatathat sends the players to the new room when it acknowledges it received the player data.
sRoomLauncher.cs
Authenticate players in the game room
Open 'sGameRoom.cs'.
Add a private dictionary field
m_playerDatathat maps tokens to usernames.Add an
Authenticatefunction 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.Log the names of eliminated players in the
FinishGamefunction.Add an
OnGetPlayerDatafunction 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
Open the 'Lobby' scene.
Build the configs (CTRL+F2).
Launch the local cluster with the same settings as before.
Make sure 'Use Live Server' is not checked when inspecting the 'Lobby Room' 'Connect' script.
Enter play mode. You will connect to the lobby.
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.
After 10 players (bots) are connected, the lobby server logs should say 'Starting room Game'.
Once the game room is started, you will load the 'Game' scene and connect to the game room.
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.
The names of all players who lost will be logged in the Game room logs.
Stop the local cluster.
Run and connect to an online cluster
Click 'Reactor->Publishing'.
Give the image a name and version and click 'Publish'.
Login to https://console.kinematicsoup.com/.
Click on your project in the 'Reactor Projects' section.
Click the 'Images' tab.
Click 'Launch' in the row for the image and version you just published.
Check 'Launch Cluster'.
Select 'Lobby' for the 'Scene'.
The 'Room' should say 'Lobby Room'
'Public Instance' should be checked.
Check 'Keep Alive'.
Give your cluster a name.
Click 'Launch'.
In Unity, make sure 'Use Live Server' is checked when inspecting the 'Lobby Room' 'Connect' script.
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
When you are done, go to the 'Sessions' tab in the web console.
Expand the cluster to see the rooms running in the cluster.
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

