Tutorial 1 Part 2 - DotNET

Technical terms used in this tutorial can be found in the glossary.

This tutorial shows:

  • ReplicaNet session finding or create.

  • Include two different C++ class object types and how to get those objects shared in a network session.

Adding session discovery or creation

Go back to editing the "main.cpp" file to contain this code.

As you can see uses quite a lot of paranoid checking to list and check session names. Lets look at a smaller code chunk below.

The code chunk below shows how to use a small loop to start a session find, wait a short time for the results to come back and then enumerate the results if there are any.

If a ReplicaNet network session is already running then the "found" string will contain the address of a session. If a session was found then the address is passed to ReplicaNet::SessionJoin() otherwise a new session is created with ReplicaNet::SessionCreate().

 

Using the new source code in "main.cpp" you should be able to compile without any errors or warnings. When you run the program by using Menu bar->Build->"Execute Tutotial1.exe" you should see this output in a window.

 

Adding game objects as C++ classes

Now we are ready to add two game class objects. In a typical game scenario you might have a C++ class per object type. This way you can define game behaviour using C++ class features. In this scenario we will have a base class called "BaseObject" that has several virtual methods for Init(), Tick() and Render(). This kind of class can be used as a common interface for all other game objects. For example:

Create "BaseObject.cpp" in a similar way to how "main.cpp" was created and added to the project, then add this code:

Next create "BaseObject.h" and add this code:

Editing main.cpp again change the code to be the code below:

The extra code added to "main.cpp" basically handles allocating some new game objects and iterating through all available game objects, very much like a game could do.

Lastly we need to create the files for our game objects.

Create "Enemy.cpp" and add this code:

Then for  "Enemy.h"

You will notice that the class "Enemy" inherits from "BaseObject" and "_RO_DO_PUBLIC_RO(Enemy)". The latter term equates to "public _RO_Enemy" and is included so that ReplicaNet knows how to control this C++ class.

Create "Player.cpp" and add this code:

Then for "Player.h":

 

You will notice that both objects use the function "SetOpaquePointer((BaseObject *)this);" in the constructor. This useful feature enables each class to set the ReplicaObject opaque pointer so that when in main.cpp the ReplicaNet object list is iterated the ReplicaObject::GetOpaquePointer() pointer result can be cast safely to "(BaseObject *)". Because the function members for the "BaseObject" are virtual the real function members are called in the "Player" and "Enemy" classes.

 

Lastly we need to create the ROL files for both objects and update the "Application.rol" file.

Create "_RO_Player.rol" and add this file to the project as before with the "Application.rol" file. Make sure the custom build rules are also set for this file. Then add this code to the file:

Do the same for the "_RO_Enemy.rol" file and add the code:

Edit "Application.rol" to be:

This tells the ReplicaNet application to register the classes "Player" and "Enemy" as network ReplicaObject classes.

Do a build now (the same way as described in Tutorial 1 part 1 and you should see that the ROL files get compiled, for example:

 

But you will get linker errors. This is because the compiled ROL file output has not been added to the project. Add the generated files "_RO_Enemy.cpp", "_RO_Enemy.h", "_RO_Player.cpp" and "_RO_Player.h" to the project.

Rebuild the project and you should not have any errors or warnings.

When you run the application you should see the output:

Which pauses and then displays a constantly updating list of game objects:

 

If you run the tutorial for a second time, while the first is running you should see some output similar to this:  

You should see this instance of the tutorial display it has found another session and attempt to join the session. As you can see the number of objects being created is twice as many as the tutorial is allocating in the code. This is because the first set of objects created are actually objects that come from the first instance of the tutorial.

Watch the output in the first instance of the tutorial and you should see:

 

This shows the session is working and that the objects created by one session are being created by ReplicaNet on the other session.

You might notice however that the "mPlayerColour = -1" line is wrong as this cannot be the real player colour.

This is because we have not yet told ReplicaNet to transmit this information. This is covered in tutorial 1 part 3.