Skip to main content

Golden Path Module Two

In this Guide we will be covering the following:

  • Network variables (client and server-controlled)
  • Network transforms
  • RPCs

Requirements#

You should have completed the foundation module here before starting this tutorial..

Create a Clone 'GoldenPath' project#

important

The clone does not support rollback so it will only work on the unity version that it was originaly created for. Attempting to open the clone on a newer or older version of Unity may cause unexpected funcationality.

We create a clone of the Golden Path foundation project that you created, if you have not yet created a GoldenPath project then you should follow the steps here to create one.

tip

Working from a clone of the initial project will help you learn about new features and concepts without having to repeat previous work.

Create a clone of GoldenPath#

  1. Navigate to where you Golden Path project is stored. For this guide we used a Mac and our path was ~/dev/mlapi-golden-path/. On a Windows machine your path may be slightly different.
  2. Create a new folder.
  3. Rename the new folder Golden Path_<modulenumber>.
    tip

    For the purposes of this series I will replace modulenumber with the current module number of the series. You do not have to though you may find it makes following the series easier.

  4. Open the Goldenpath folder.
  5. Select Copy.
  6. Select the folder containg your Golden Path project.
  7. Select Paste.
tip

I moved my new folder to keep my file structure clean its not a required step, but it does look nice.

Add the clone to Unity Hub#

  1. Open Unity Hub.
  2. Click Add.
  3. Navigate to where the clone was saved.
  4. Select GoldenPath_<modulenumber>.
  5. Click Open.

The Clone should now appear in the project list of Unity Hub as GoldenPath_<modulenumber>.

Open GoldenPath_Two#

  1. Open Unity Hub.
  2. Select GoldenPath_Two from the list of projects displayed.

Introducing a Server-controlled Network Variable#

This section adds a Server-controlled Network Variable to the project.

  1. Open the Scripts Folder.
  2. Create a script called NetworkVariableTest.
  3. Click the Player prefab.
  4. In the Player prefab Inspector tab, click Add Component.
  5. Click Scripts, and add the NetworkVariableTest.cs script you created earlier.
  6. Open the NetworkVariableTest.cs script.
  7. Edit the NetworkVariableTest.cs script to match the following.
Click to show/hide the Code.
using MLAPI;
using MLAPI.NetworkVariable;
using UnityEngine;
public class NetworkVariableTest : NetworkBehaviour
{
private NetworkVariable<float> ServerNetworkVariable = new NetworkVariable<float>();
private NetworkVariable<float> ClientNetworkVariable = new NetworkVariable<float>();
private float last_t = 0.0f;
void Start()
{
ClientNetworkVariable.Settings.WritePermission = NetworkVariablePermission.OwnerOnly;
ClientNetworkVariable.Settings.ReadPermission = NetworkVariablePermission.ServerOnly;
if (IsServer)
{
ServerNetworkVariable.Value = 0.0f;
Debug.Log("Server's var initialized to: " + ServerNetworkVariable.Value);
}
else if (IsClient)
{
ClientNetworkVariable.Value = 0.0f;
Debug.Log("Client's var initialized to: " + ClientNetworkVariable.Value);
}
}
void Update()
{
var t_now = Time.time;
if (IsServer)
{
ServerNetworkVariable.Value = ServerNetworkVariable.Value + 0.1f;
if (t_now - last_t > 0.5f)
{
last_t = t_now;
Debug.Log("Server set its var to: " + ServerNetworkVariable.Value + ", has client var at: " +
ClientNetworkVariable.Value);
}
}
else if (IsClient)
{
ClientNetworkVariable.Value = ClientNetworkVariable.Value + 0.1f;
if (t_now - last_t > 0.5f)
{
last_t = t_now;
Debug.Log("Client set its var to: " + ClientNetworkVariable.Value + ", has server var at: " +
ServerNetworkVariable.Value);
}
}
}
}
  1. Save your scene.

Testing Server-controlled Network Variables#

Now we will test the Server-controlled Network Variable works as we intended.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. You should see the following in the console, showing that the server and client are sharing the variable:
Server's var initialized to: 0
Client's var initialized to: 0
Server set its var to: 0.1, has client var at: 0
Client set its var to: 0.1, has server var at: 0.2
Server set its var to: 3.099999, has client var at: 2.6
Client set its var to: 3.099999, has server var at: 3.199999
Server set its var to: 6.099997, has client var at: 5.599997
note

Since the printing to the terminal does not happen on every tick, the numbers will not match up perfectly.

Introducing Network Transform#

This section adds a Network Transform component that will move the player.

  1. Click Player prefab.
  2. Click Add Component in the Inspector Tab.
  3. Select MLAPI from the list shown.
  4. Select the Network Transform component from the list shown.
  5. Open the Scripts Folder.
  6. Create a script called NetworkTransformTest.
  7. Click the Player prefab.
  8. In the Player prefab Inspector tab, click Add Component.
  9. Click Scripts, and add the NetworkTransformTest.cs script you created earlier.
  10. Open the NetworkTransformTest.cs script.
  11. Edit the NetworkTransformTest.cs script to match the following.
Click to show/hide the Code.
using System;
using MLAPI;
using UnityEngine;
public class NetworkTransformTest : NetworkBehaviour
{
void Update()
{
if (IsClient)
{
float theta = Time.frameCount / 10.0f;
transform.position = new Vector3((float) Math.Cos(theta), 0.0f, (float) Math.Sin(theta));
}
}
}
  1. Save your scene.

Testing Network Transform#

Now we check that the Network Transform functions correctly.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. You should see the player capsule moving in a circle on both the client and the server.

Introducing RPCs#

This section adds some basic RPCs to the project.

  1. Open the Scripts Folder.
  2. Create a script called RpcTest.
  3. Click the Player prefab.
  4. In the Player prefab Inspector tab, click Add Component.
  5. Click Scripts, and add the RpcTest.cs script you created earlier.
  6. Right Click Player prefab.
  7. Open the RpcTest.cs script.
  8. Edit the RpcTest.cs script to match the following.
Click to show/hide the Code.
using MLAPI;
using MLAPI.Messaging;
using UnityEngine;
public class RpcTest : NetworkBehaviour
{
private bool firstTime = true;
[ClientRpc]
void TestClientRpc(int value)
{
if (IsClient)
{
Debug.Log("Client Received the RPC #" + value);
TestServerRpc(value + 1);
}
}
[ServerRpc]
void TestServerRpc(int value)
{
if (IsServer)
{
Debug.Log("Server Received the RPC #" + value);
TestClientRpc(value);
}
}
// Update is called once per frame
void Update()
{
if (IsClient && firstTime)
{
firstTime = false;
TestServerRpc(0);
}
}
}
  1. Save your scene.

Testing RPCs#

Now we will test that the client and server are both recieving the RPCs correctly.

  1. Select File > Build and Run.
  2. Stop the player.
  3. Launch the client and server together in a terminal as shown in Testing the command line helper.
  4. After a brief delay, the client and server will spawn.
  5. In the console, you should expect to see the client and server sending RPC messages to each other.
  6. The client kicks off the exchange in its Update call the first time with a counter value of 0.
  7. It then makes an RPC call to the server with the next value. The server receives this and calls the client. In the console view, you will see:
Server Received the RPC #1
Client Received the RPC #1
Server Received the RPC #2
Client Received the RPC #2
Server Received the RPC #3
Client Received the RPC #3
...
Congrats!

Congratulations you have learned how to use some of the basic building blocks of Multiplayer game creation.

Next Steps#

For more information on the relevant concepts introduced here please refer to the following sections of the documentation:

Special Thanks

This guide would not have been possible without the hard work and support of Matt Walsh, Unity.