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.

Posted in Dark Rift 2 and tagged , .

4 Comments

  1. MonoBehaviourSingletonPersistent

    you never mentioned that before, this is part 6 and its mentioned in 7.

    got me confused, maybe fix that

    • Hello, I will take a look on this and will update the article if need.
      You can find the MinoBehaviourSingletonPersistent on my github

  2. Question on using MonoBehaviourSingletonPersistent.

    I have downloaded the Utilities folder from your GitHub page.

    Where do I put it and or what do I do with these scripts?

Leave a Reply

Your email address will not be published. Required fields are marked *