DarkRift 2 Tutorial for Unity 3D – Part 3 – Architecture and main game scene

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

Unity game architecture with DR2

There are a lots of way to design the game architecture in unity when working in a client/server environnement. It’s very important to think efficiently your architecture, otherwise you can be face of a wall at a certain point of achievement of your project.
In fact, when working with a DR2 unity embedded server, the common difficulty is to keep the scene equal between the server and the client.

For the tutorial, i will use the following implementation :

There are 3 blocks wich represents :

  • Yellow : The server scene
  • Blue : The client scene
  • Green : All the game scene of our project

How they will work together ?

Server Side

When we’ll launch our server, we’ll start the MasterServerScene (wich includes the DR2 server). This scene will load an additive specified scene that contains our game, and that’s all.

Client side

It will be exactly the same way of working

The first advantage of this architecture is that we are sure that the server and the client have the same game scene !

Create the main game scene

First of all, we need to create the main scene of our game. We’ll keep it simple by creating a scene with :

  • A basic platform
  • A bouncing ball (using the Unity 3D physics engine)

Let’s start by creating a simple scene named MainGameScene in the “Scenes” folder

As said, this scene will be shared by both server and client.

Create the platform

Create the platform by right clicking and add a 3D object > Cube

Name it “Platform” and fill the transform with the following values :

Position (0,-4,0) - Scale : (10,1,5)

Create the ball

First thing to make our ball bounce is to create a Physic Material that the “Sphere Collider” will use. So let’s create a new folder named “Physic Materials” and create a new Physic Material

Call it “BouncyBall” and configure it like that

Now, on the scene hierarchy, create a new game object called “BouncyBall

Select the “BouncyBall” object in the scene, and add a Rigidbody component to this object

Finally, we just need to assign the material to the material property within the Sphere Collider component. Go to your Physic Materials folder, and drag the BouncyBall material into the material property of the component.

Ok, if you run the scene, you will see your ball bouncing, but, it’s a little dark, isn’t it ? Let’s polish a little the game. Add a directional light and configure it like that :

And the camera as the screen below :

The result must be something like that :

The bouncing ball that we’ll synchronize with DR2

OK ! Well done, we now have the main scene game ! I totally agree with you, the scene has no interest but it’s an exemple to illustrate how the server will synchronize the bouncing ball with all the connected clients.

Yes, we will synchronize object based on physics ! That’s a good news that i havn’t shared you, isn’t it ? Our server will be authoritative.

Definition : An authoritative server doesn't let the client determines positions, action effect, ... That's the server role. Each synchronized objects are moved, affected, ... on the server. The server then sends data to clients.

On the next article, we’ll setup the ServerMasterScene with DR2.

DarkRift 2 Tutorial for Unity 3D – Part 2 – Get DarkRift2

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

Create an empty scene and download the asset

So, let’s start with a step by step to create the environnement that will be used for the whole entire tutorial.

For this tutorial, i’m using the 2018.3.1f1 version of Unity.

  • Open Unity, and create a new empty project, Go to the asset store, and look for DarkRift2 :

As explainded into my first article , the PRO version is not free, so we’ll use the free version of DR2, wich perfectly fits our needs in this context.

  • Get it, Import it and in the “Import Unity Package” windows, let all checkbox checked :

You can now see in the Asset folder a lot of you content. Let’s describe them just below :

  • DarkRift : Contains a .ZIP wich embbed the .DLL to develop a console server outside Unity. In our case, it will be useless because we’ll make an Unity Embedded Server.
  • 1 ChatDemo, 2 DragDemo, 3BlockDemo : These folders are examples provided by DarkRift. Feel free to open them and take a look. They are very basics, but very instructives too !
  • DarkRift / DarkRift / Plugins : this folder contains all unity components that we need to create our server / client.

DarkRift2 Unity Components

Server side

XmlUnityServer

  • This is THE component that, attached to a GameObject, will act as a server. When attached and game launched, the server will be automatically created and will listen for each connections, messages, … and so on.
Note : UnityServer component is deprecated, don't use it

UnityConsoleWriter

  • This component is used to create message visible through the console on unity and enable us to debug more easely.

UnityServerHelper

  • Static classes/methods wich are helpers and used by DarkRift2. You can use them too.

Client side

UnityClient

  • As you can see, there is only one unity component for the client : UnityClient. Attacehd to a GameObject, this component will allow you to communicate with a DarkRift2 server.

As the server, the client is automatically created and connects to the server (with a specific [IP:PORT], by default it’s the lookup adress wich is : 127.0.0.1 and port 4296)

What’s next article about ?

The overview of the content of the DR2 asset is finished. The scene to be worked. The next steps are

  • create the scene that will embbed the server
  • Test the server
  • Get output to the console
  • Build our server in both normal & headless mode (Whitout graphics)

Stay connected !

DarkRift 2 Tutorial for Unity 3D – Part 1 – Concepts

DarkRift 2 Tutorial for Unity 3D

What is it ?

Reminder : You can find all the DarkRift2 related articles here 
You can find the entire project on my official GitHub
DarkRift2 (DR2) is an asset developped by “Darkrift” designed to give to your game a multiplayer dimension very easely.

This is the second version of their product, wich is more fast and reliable than the first one. They took the best from DarkRift and they improved it on DarkRift2.

You can find here the website : https://darkriftnetworking.com/DarkRift2

Dark Rift 2 is a librairy wich allow you to send message through network simply and fastly. It’s an abstraction for sending messages over TCP and UDP. It’s a High Level API. It means that you just have to use classes and functions provided in the library to send message. You don’t need to create you TCP message from scratch ! 

DR2 is very interresting regarding it’s price. There is 2 version : 
  • Free : You can use it freely (unlimited users)
  • Pro (90$) : This version provides you some extra feature (Matchmaking, Rooms, ..) and you have a complete access to the source code.
DR2 is extremelly well documented. You can fin all the stuff you need here : 

How to implements Dark Rift ?

There is two way of implementing DR2 with unity.
  1. Using the server console (Provided by Dark Rift)
  2. Using the XML Server Unity Component provided by Dark Rift too…
With the first solution, you’ll need to create your Dark Rift Plugins to handle the server logic. There is a complete exemple on how to create your first plugin with DarkRift. 
The second way is to use the XML Server Unity component on a scene, wich means that a scene will act as server. 
There is pros and cons for each solution. The main pros for using the XML Server Unity is that this component allows us to us the powerful of the Unity engine (Physics, NavPath, …)

Very simple schema of a Dark Rift 2 communication

Here is a very simplified exemple of Client / Server communication. By using Dark Rift, all that is red surrouded is automaticcaly handled by DR2 without doing anything !
In green, that’s what we have to code with DR2 for sending message (You can either receive message, wich is very similar)

For what a message is supposed to be used ?

Message is information shared by a client and a server (for exemple to login) or by all clients (sharing the player position)
A client never send message to another client. It send message to the server, wich broadcast the message to all clients.
As you understood, message are used to share information between connected clients and server act as the broadcaster 
In the next article, we’ll see how to setup the Unity environnement.
Stay tuned !