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

Networked Properties and RPCS

Summary

This tutorial demonstrates the use of RPCs and properties as ways to sync information between the server and clients. RPCs will be used to send messages between tagged methods while properties will be used to sync simple data values. A linear predictor asset will be created and configured to interpolate a color property. Properties are associated with an entity, player, or the room and are synced to new players when they connect. Use them for data you want synced to everyone such as player/enemy health. RPCs are one-time events used to send data from client to server or server to client and invoke functions on a remote client or server, and unlike properties, can be sent to individual clients. Use them for one-time events, such as informing clients of where a bullet hit so they can render a particle effect and/or play a sound there, or for data you want sent only to specific clients, such as an individual player's ammo.

Requirements

Setup

Create a Room

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

  2. Set the gravity in the inspector for the ksPhysicsSettings to (0, -3, 0).

Create a Cube Entity Prefab

  1. Create a cube.

  2. Add a ksEntityComponent.

  3. Add a Rigidbody component.

  4. Make the cube a prefab in the 'Resources' folder.

  5. Delete the cube from the hierarachy.

Create a Linear Predictor to Interpolate the Color Property

Predictors are used to interpolate/predict entity transforms and entity, room, or player properties. We will create a ksLinearPredictorAsset and configure it to interpolate a color property on the 'Cube' entities. For an example on predictors, see here.

  1. Right-click in the project browser and select 'Create->Reactor->Linear Predictor.

  2. Name the predictor 'Color Predictor'.

  3. Expand the 'Predicted Properties' section in the inspector and click the + button to add a new predicted property.

  4. Set the 'Property Id' to zero. This will be the id of the color property.

  5. Set the 'Type' to 'Linear Color'. This tells the predictor to interpret property 0 as a color and to linearly interpolate it.

  6. Select the 'Cube' prefab. In the ksEntityComponent inspector, check the box for 'Predictor', then set the 'Predictor' to the 'Color Predictor' you just created.

    • This makes the entity use the predictor you created to linearly interpolate the transform and property 0 as a color, instead of just the transform.

    • The 'Controller Predictor' is used instead of the 'Predictor' when the entity has a player controller with ksPlayerController.UseInputPredictor set to true. Only entities controlled by the local player will have player controllers.

Scripting

Create a Class for Constants

The consts script defines const ids for properties and RPCs used on both the client and the server. The id of the COLOR property must be the same as the property id we set in the 'Color Predictor' we created earlier.

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

    • All scripts in common folders are available on both the client and the server, and may not contain any Unity references.

    • To create a common folder, first create a folder. Then right-click in that folder in the project browser and select 'Create->Assembly Defintion Reference'. In the inspector for the assembly reference, set 'Assembly Defintion' to 'KSScripts-Common'

Consts.cs

Create and Attach a Server Entity Script to the Cube Prefab

The server cube script sets a color property on the cube. Every second, the script chooses a new color and interpolates the color property towards the new color.

Every room, player, and entity has a collection of properties that are synced from the server to clients. Properties are accessed by a uint key, and property values are ksMultiTypes. ksMultiTypes can store most basic types and common structs, and have implicit conversions between these types for convenience. The following types are supported:

  • byte

  • short

  • ushort

  • int

  • uint

  • long

  • ulong

  • float

  • double

  • char

  • bool

  • string

  • ksVector2 and Vector2

  • ksVector3 and Vector3

  • ksQuaternion and Quaternion

  • ksColor and Color

  • ksVector2Int and Vector2Int

  • ksVector3Int and Vector3Int

  • arrays of the above types

You can also convert to and from classes that implement ksISerializable using ksMultiType.FromSerializable(ksISerializable) and ksMultiType.ToSerializable<T>().

  1. Select the 'Cube' prefab.

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

  3. Name the script 'ServerCube'.

ServerCube.cs

Create a Fader MonoBehaviour

The fader fades the alpha on the object's material to zero, then deletes the game object.

  1. Create a new MonoBehaviour named 'Fader'.

Fader.cs

Create and Attach a Client Cube Script to the Cube Prefab

The client cube script sets the color of the cube to the synced color property. The script fades out and destroys the game object by attaching a Fader when it receives a fade entity RPC from the server.

In the first tutorial, we used a client-to-server RPC to tell the server to spawn a cube. The server can also send RPCs to clients. There are two kinds of RPCs available to both the server and the client: room RPCs, and entity RPCs. Room RPCs are called on the room. The handler functions tagged with ksRPCAttributes must be in room scripts. The RPC used in the first tutorial was a client-to-server room RPC. Entity RPCs are called on an entity and the handler functions tagged with ksRPCAttributes must be in entity scripts on that entity.

  1. Select the 'Cube' prefab.

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

  3. Name the script 'ClientCube'.

  4. Create a public material field named 'FadeMaterial'. In the inspector, assign it the 'Sprites-Default' material.

ClientCube.cs

Create a Server Player Script to Manage Ammo

Just like rooms and entities, players can have scripts too. The server player script tracks how much ammo a player has. A Reload function starts a timer and the Update function maxes out the player's ammo when the timer reaches zero. An RPC is used to tell players how much ammo they have.

The proxy MonoBehaviours for player scripts are attached to the game object with the ksRoomType component like room script proxies, but unlike room scripts, they are attached to the ksPlayer object on the client or ksIServerPlayer object on the server that gets created for each player intead of the room object.

There are three overloads of the CallRPC method on the server for specifying which clients receive the RPC. One takes a ksIServerPlayer as the first parameter that sends an RPC to only one player. One takes an IList of ksIServerPlayers to send the RPC to, and one does not take any player arguments and sends the RPC to all clients.

RPCs calls can have any number of ksMultiType arguments after the RPC id.

We use an RPC to inform the player of how much ammo they have rather than syncing it as a property because clients only need to know about their own ammo. Properties are synced to everyone, whereas RPCs can be sent to individual clients. When a player joins, all properties are synced to them, whereas RPCs are one-time events that are only sent to connected clients at the time of the RPC call.

We only need to send an ammo RPC to the client when they first connect and when they reload. The client already knows it loses one ammo every time it shoots, so we don't need to send an RPC to tell it that.

  1. Select the 'Room' game object.

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

  3. Name the script 'ServerPlayer'.

ServerPlayer.cs

Create a Server Room Script

The server room script assigns players a random position property when they connect. It continuously spawns cubes that fall from the sky. It does a sphere sweep when it receives a shoot RPC from a client if they have any ammo. It calls an entity RPC on any entities hit by the sphere sweep, then destroys the entity.

  1. Select the 'Room' object.

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

  3. Name the script 'ServerRoom'.

ServerRoom.cs

Create a Client Room Script

The client room script sends an RPC to the server to shoot when the mouse is clicked and the client has ammo. It decrements its ammo each time is shoots. It sets the camera position based on the local player's position property. It logs messages when the local player's score or ammo changes.

  1. Select the 'Room' object.

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

  3. Name the script 'ClientRoom'.

ClientRoom.cs

Testing

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

  2. Start a local server.

  3. Enter play mode.

You should see colorful cubes slowly falling from the sky. The cubes will slowly change color. When you click, the server will do a sphere sweep in the direction you clicked if you have ammo and destroy any cubes that you hit. When you shoot a cube, the cube will fade away and your score will be logged on the client. When you run out of ammo, you'll get your ammo back after five seconds.

Last updated