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

Basic Orchestration Using the Cluster

Summary

This tutorial demonstrates how to use a Reactor cluster to manage multiple room instances. You will create a lobby room that accepts new player connections and then moves those players to different game rooms. The lobby will also manage the starting and stopping of game rooms to accomodate those players.

Requirements

Setup

  1. Create a new scene named 'ClusterTutorial'.

Create a Lobby Room

  1. Right click in the hierarachy window and create a 'Reactor/Room' game object.

  2. Name the game object 'LobbyRoom'.

  3. Remove the ksConnect component.

Create a Game Room

  1. Right click in the hierarachy window and create a 'Reactor/Room' game object.

  2. Name the game object 'GameRoom'.

  3. Remove the ksConnect component.

It's possible to have more than one room type in the same scene. This is why when you start a room, you must specify which room type to run.

Scripting

Create a Class for Constants

The consts script defines const ids for RPCs used on both the client and the server.

  1. In 'Assets/ReactorScripts/Common', create a script named 'Consts'.

Consts.cs

Create a Server Game Script

The server game room script updates the number of connected players in the room's PublicData. It adds the room tag "Game" to the room to identify this room as a game room. It shuts down the room when a shut down RPC is received from a client.

  1. Select the 'GameRoom' object.

  2. In the 'Add Component' menu, select 'Reactor->New Server Room Script'.

  3. Name the script 'ServerGameRoom'.

ServerGameRoom.cs

Create a Server Lobby Room Script

The server lobby room script queries the cluster for the list of rooms with the tag "Game" every ten seconds. It starts or stops game rooms to reach the target number of game rooms, which clients can set using an RPC to a value between 0 and 3. It gets the number of players connected to each game room from the room's 'PublicData' and only stops rooms that have no players. When clients request to join a game room, the lobby responds by sending the room information for a random game room to join.

  1. Select the 'LobbyRoom' object.

  2. In the 'Add Component' menu, select 'Reactor->New Server Room Script.

  3. Name the script 'ServerLobbyRoom'.

ServerLobbyRoom.cs

Create a Connect Script

The connect script has a ConnectToServer function that takes a ksRoomInfo describing the room to connect to which can be called from other scripts to connect to a different room. It reconnects to the room it is attached to (the lobby) if it disconnects from a different room (a game room).

  1. Create a Monobehaviour and name it 'Connect'.

  2. Attach it to your 'LobbyRoom' object.

Connect.cs

Create a Client Lobby Room Script

The client lobby room script sends an RPC to the server when 0 - 3 is pressed to set the desired number of game rooms to the pressed number. It sends a different RPC to request a game room to join when J is pressed. It connects to the game room the server sends when it responds with an RPC.

  1. Select the 'LobbyRoom' object.

  2. In the 'Add Component' menu, select 'Reactor->New Client Room Script'.

  3. Name the script 'ClientLobbyRoom'.

ClientLobbyRoom.cs

Create a Client Game Room Script

The client game room script disconnects from the game room and reconnects to the lobby when L is pressed. It sends an RPC to shut down the server when S is pressed, which will cause the connect script to reconnect to the lobby when the room shuts down.

  1. Select the 'GameRoom' object.

  2. In the 'Add Component' menu, select 'Reactor/New Client Room Script'.

  3. Name the script 'ClientGameRoom'.

ClientGameRoom.cs

Testing

Testing will be a bit different than for earlier tutorials, since we need to launch a cluster with the room to use cluster features.

Local Cluster

  1. Build your scene config (CTRL + F2).

  2. Open the 'Menu Bar->Reactor' menu and select 'Launch Local Cluster' to open the local cluster window.

  3. Set the launch scene to 'ClusterTutorial' and the launch room to 'LobbyRoom'.

  4. Click 'Launch Local Cluster'.

    • This will launch the local cluster along with a 'LobbyRoom' running the 'ClusterTutorial' scene.

    • The 'Server Logs' window will show the cluster logs. If you select the drop down for the log file, you will see three logs with a '*' after their name indicating a running server. One is for the cluster and one is the 'LobbyRoom', and one is a 'GameRoom' the 'LobbyRoom' started. Use this to switch which room's logs you are looking at.

  5. Enter play mode to connect to the lobby.

    • The server logs for the lobby will print the number of game rooms and the number of connected players across all rooms (games and lobby).

    • While connected to the lobby, you can press the number keys 0 - 3 to set the desired number of game rooms to that number. The next time the lobby refreshes the room list after you change the desired number of game rooms, the lobby will log that it is starting or stopping rooms.

    • When you press J from in the lobby, you will leave the lobby and join a random game room, as long as at least one game room is running. The Unity logs will show a message when you connect to a room showing the scene name and room type.

    • When you press L from within a game room, you will leave the game room and return to the lobby.

    • When you press S from within a game room, you will shut down the game room and return to the lobby. The lobby will start a new game room to replace the one that you shut down.

  6. When you are done testing, stop the cluster by opening the local cluster window and clicking 'Stop Local Cluster', or by closing the cluster process window.

Online Cluster

  1. Open the **'Menu Bar->Reactor->Publishing'.

  2. Login using your KinematicSoup account.

  3. Enter an image name and version in the form and click the 'Publish' button.

  4. Clusters can only be launched via the web console. Open https://console.kinematicsoup.com and login to your KinematicSoup account.

  5. Select your project from the 'Projects' or 'Shared Projects' panel.

  6. Select the 'Images' tab.

  7. Click 'Launch' next to the image you just published.

  8. Configure the launch options for the Cluster.

    • Server Type is either on demand (shared) or dedicated. Dedicated servers are only available at the Studio or Enterprise tiers.

    • Location is used to select the geographic region where the server will be hosted.

    • Provider is used to select which cloud provider will host the server.

    • Instance Size is used to select the resource limits for your server. This option is only available if 'Launch Cluster' is unchecked.

    • Launch Cluster determines if we are launching a cluster or a stand-alone server. Make sure this is checked.

    • Instance Count is the number of rooms to launch with the cluster. This option is only available if 'Launch Cluster' is checked.

    • Scene contains a list of Unity scenes available to the image. Make sure 'ClusterTutorial' is selected.

    • Room contains a list of Rooms available in a scene. Make sure 'LobbyRoom' is selected.

    • Keep Alive will restart servers if they stop unexpectedly. It is recommended that you test servers for common errors before enabling this option.

    • Public Instance will allow clients to find this server in a list of public instances.

    • Name is the name you want to give the cluster (or server if launching without a cluster).

    • Tags allows you to specify a list of room tags for the server seperated by a ';'.

  9. Click 'Launch' to launch the cluster.

  10. To connect to the online cluster, uncheck 'Use Local Server' in the inspector for the 'Connect' script and enter play mode.

  11. Click the 'Sessions' panel in the web console. You will see a list of running stand-alone servers and clusters. Clusters can be expanded to view rooms running in those clusters. Refresh the page if you don't see any rooms running under the cluster. You can view logs for the running rooms by clicking the icon next to the room in the 'Log' column.

  12. When you are done testing, stop the cluster by clicking the 'X' in the 'Stop' column next to the cluster in the 'Sessions' panel.

Last updated