DarkRift 2 Tutorial for Unity 3D – Part 6 – Create the client

Reminder : You can find all the DarkRift2 related articles here 
You can find the entire project on my official GitHub

This article deals with the client implementation within Unity 3D. Here are the steps we’ll accomplish :

  • Create the client scene
  • Create the DarkRift2 client game object
  • Load the main game scene
  • Try to connect to the server (built on the last article)

Create the client scene

Open the project and create a new scene called “MainClientScene“. As for the server, we can delete the camera because the camera is contained on the MainGameScene.

Create a new GameObject “ClientManager” in the hierarchy :

Add a new component Client wich is the official DarkRift2 client :

The DarkRift2 Client

By adding the component on the GameObject, you saw that there is a lot of properties. Let’s talk about them :

  • Adress : IP adress of the DarkRift2 Server (127.0.0.1 is the lookup adress wich reference the current machine)
  • Port : Port number of the server
  • IPVersion : You can choose between IPV4 or IPV6
  • InvokeFromDispatcher : As you know, there is a dispatcher. You can invoke from it
  • SniffData : Print all messages into the console
  • Cache : Some settings to adjust for the cache system

For the tutorial, we can let it like that because the server we ran in the last article had this adress : 127.0.0.1:4296

Create a custom ClientManager

As for the server again, we’ll create a new script in the Network folder called ClientManager. It will handle our logic for our game regarding the client (What to do on connection, disconnect, …)

Create the ClientManager and add it to the GameObject ClientManager. For now, we just need a reference to the Client (DarkRift2).

First of all, you need to download my Utilies in order to use the script MonoBehaviourSingletonPersistent wich when is inherited, it implements the Singleton pattern. Persistent means that it doesn’t destroy on load.

Here is the link : https://github.com/TheUncas/unity3D-tools
Then, you can write the following code for the ClientManager :

using DarkRift.Client.Unity;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ClientManager : MonoBehaviourSingletonPersistent<ClientManager>
{

    #region Properties

    /// <summary>
    /// Reference to the DarkRift2 client
    /// </summary>
    public UnityClient clientReference;

    #endregion

    #region Unity Callbacks
    private void Awake()
    {
        base.Awake();

        //////////////////
        /// Properties initialization
        clientReference = GetComponent<UnityClient>();
    }

    // Start is called before the first frame update
    void Start()
    {
        //////////////////
        /// Load the game scene
        SceneManager.LoadScene("MainGameScene", LoadSceneMode.Additive);
    }
    #endregion
}

Don’t forget to use the DarkRift.Client.Unity namespace !

Try the client connection

All is ready to be tested, do you think that we can lauch the client right now ? Maybe.. let’s try :

Seems to work. The MainGameScene is correctly loaded but if you look at the console window, you will notice that there is an error. The client cannot connect to the server.

This error occurs only if the server is unreachable. In my case (and maybe yours) i don’t have launched the server before lauching the client. Of course, it’s basic. If the client need to connects to the server, the server must be turned on before the client try to connect.

Start the server with the build we made on the last article and try to restart the client.

We did it right. The clients succeded to connect to the server. And if you go back to the server windows process, you will notice that the server displayed a message :

Yes, it informs you that a new client has connected ! Perfect, isn’t it ? …. but, wait… is the ball synchronized ?

Of course not ! we’ve just handle the client connection. We havn’t send any message to client about the ball position… And the client doesn’t know read message from the server for now.

What’s next ?

On the next article, we’ll explain how we can synchronize object of the MainGameScene and how we will do it… a lot of work is waiting for us but we are on the good way !

Thanks for reading.

DarkRift 2 Tutorial for Unity 3D – Part 5 – Build the server and load game scene

Reminder : You can find all the DarkRift2 related articles here 
You can find the entire project on my official GitHub

Load the game scene

In the last article, we’ve built our server scene. As you surely know, this scene needs to load the shared game scene between the server and the client. So, we just need to include the following namespace in the GameServerManager script :

using UnityEngine.SceneManagement;

and load the scene on the Start() function :

//////////////////
/// Load the game scene
SceneManager.LoadScene("MainGameScene", LoadSceneMode.Additive);

The LoasScene method of the SceneManager takes 2 parameters which are :

  • The name of the scene.
  • The loading mode. If we don’t specify the Additive mode, Unity will remove from the game all opened scenes and load the scene “MainGameScene” alone

It’s not finished, in order to use the correctly the SceneManager, all scenes loaded by this classe need to be added to the build settings. So, follow these steps :


Close the Build Settings window. Let’s run the game. You can see that the MainGameScene is now loaded correctly in the hierarchy.

Build the server

For now, if you have already built games on windows (or another OS), you know that what you see on unity will be displayed on a window on your computer. That’s fine if you need to test your game or build a client version, but for a server, it’s inappropriate.

Yes, the server process doesn’t need to display graphics, play sounds, …, it just needs to handle the game in a background process. Desactivating graphics and sounds for example will reduce the need of resource. By doing that, you will be able to run more servers on the same machine.

Unity provide a “Headless mode” for that. It’s specialy recommended when you build servers.

First step is to add the MainServerScene into the Build Settings. After that, you need to put the server scene at the first place of the build. Like that, when the build will be executed, it will launch the first scene in the Build Settings, to know : MainServerScene :

Select a destination folder for the build, and launch the .exe

Oh damn ! the build seems to execute but throw an exception ! By reading the message, it seems that there is an issue with the DR2 library… the error is :

NotSupportedException: Specified method is not supported.
  at System.Threading.Mutex..ctor (System.Boolean initiallyOwned, System.String name) [0x00006] in <0000d56de0ae43ca875d7babfd990580>:0
  at (wrapper remoting-invoke-with-check) System.Threading.Mutex..ctor(bool,string)
  at DarkRift.Server.DataManager..ctor (DarkRift.Server.DarkRiftServer server, DarkRift.Server.ServerSpawnData+DataSettings settings) [0x0006f] in <56e64ae805e047608792040aeb2db28d>:0
  at DarkRift.Server.DarkRiftServer..ctor (DarkRift.Server.ServerSpawnData spawnData) [0x00024] in <56e64ae805e047608792040aeb2db28d>:0
  at DarkRift.Server.Unity.XmlUnityServer.Create (System.Collections.Specialized.NameValueCollection variables) [0x0007f] in <88a1dbe1fedd44a6993e063fea92a13f>:0
  at DarkRift.Server.Unity.XmlUnityServer.Create () [0x00006] in <88a1dbe1fedd44a6993e063fea92a13f>:0
  at DarkRift.Server.Unity.XmlUnityServer.OnEnable () [0x00008] in <88a1dbe1fedd44a6993e063fea92a13f>:0

Ok, don’t worry, it’s only because the “mutex” class used in DarkRift2 is not compatible with the current “Api Compatibility Level*” parameter. So, open the Build Settings window, and open Player Settings :

Scroll down and change the value to “.NET 4.x” and rebuild the server. Launch it :

Authorize the application to communicate accross the network. And now, you cannot see any error ! Even better, if you read well, you can see a message informing you that the server is listening :

[Info]    DefaultNetworkListener Server mounted, listening on port 4296.

What next ?

On the next article, we will create the client and load the game scene too. It will be relatively simple because it will be extremely closed to what we’ve done in this article. So, if you think that you can do it alone wihtout my help, you can skip the part 6 and go directly to the part 7 !

Thanks you for reading.