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 :
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.