Publicly-Visible Data for Rooms
Summary
Use public tags and data to share information to users before they connect to a server. This tutorial uses public data to report the number of connected users and uses public tags to mark empty rooms. You can use this to display the number of players in each room, or any other information you want, in your UI for picking a room to connect to.
Requirements
Completion of Tutorial 1
Setup
Create a Room
Right click in the hierarachy window and create a 'Reactor->Room' game object.
Scripting
Create a Server Room Script to Manage Public Data
The server room script stores the number of connected players in the room's public data, and adds or removes a public room tag named "empty" depending on if the the room is empty (has no connections) or not. This data is publically available without having to connect to the room in the response to a ksReactor.GetServers call.
'PublicData' is a ksJSON object and can store anything you could store in JSON format. 'PublicTags' is a ksHashSet of strings. While 'PublicData' is more flexible in the types of data it can store, 'PublicTags' can be used with the cluster to filter rooms by tag, though that is outside the scope of this tutorial.
Select the 'Room' object.
In the 'Add Component' menu, select 'Reactor->New Server Room Script'.
Name the script 'ServerRoom'.
ServerRoom.cs
Create a ConnectHandler Script
This script handles ksConnect events and polls the online services for an updated list of rooms every 10 seonds. When a list of rooms is fetched it will log a summary of the rooms, including the 'PublicTags' and the number of connections we set in the 'PublicData'. When space is pressed, it connects to the first room in the list, or disconnects if there is already a room connection.
Create a new MonoBehaviour named 'ConnectHandler'.
Attach it to the 'Room' game object.
Edit the file to match the code below.
Select the ksConnect component on the room and add the 'ConnectHandler' 'OnGetRooms' method to the ksConnect 'OnGetRooms' event list.
Click the '+' button in ksConnect 'OnGetRooms' event list.
Drag the Room instance into the new object field.
Select 'ConnectHandler->OnGetRooms' in the function selector.
Set the ksConnect 'Connect Mode' property to 'ONLINE'.
ConnectHandler.cs
Testing
You cannot query a list of local servers to view their public data, so to test this you'll need to deploy live servers.
Open the 'Menu Bar->Reactor->Publishing' menu.
Login using your KinematicSoup account.
Enter an image name and version in the form and click the 'Publish' button.
Open the 'Menu Bar->Reactor->Servers' menu.
Select your scene from the 'Scene' dropdown.
Check the 'Is Public' property and provide a name for this instance, then click the 'Launch Server' button.
After a moment your server should appear in a list of running instances.
Once the server is started, select the room and change the 'Connect Mode' property of the ksConnect component to 'ONLINE'.
Enter play mode. Every 10 seconds, you should see a summary of the running rooms in the Unity logs, which will include the number of connections and the tags. Click on the log entry to see the full summary. While you are not connected, it should show 0 connections and the tag "empty".
If you enter play mode before the server finishes launching, you will see a warning in the logs saying "No servers found.". Leave play mode and try again once the server is started.
Pressing space should connect you to the room. Pressing space again should disconnect you. While you are connected, the next time the room summary prints it should show 1 connection and no tags.
Once you are done testing, open the 'Servers' window and stop the server instance.
Last updated

