Dedicated server code split from client code

Ok, my question is basically, how can i split code that needs to be in the game, but basically server side only from my build?, for instance i will have a persistent database per game, but i do not want clients to be able to host games, i want them to be ran on dedicated servers only, so how can i exclude say, my C++ database code, amung other things from my builds when it comes to compile/testing

1 Like

The key to this is the Server and Client code configuration. So to build a cooked client that doesn’t have access to dedicated server code you can build “Development Client”, which is almost the same as “Development” but with server-only code disabled. What this does is set the WITH_SERVER_CODE define to false. When this is false various things like server-side networking are compiled out in the engine. There is no client-only configuration with editor functionality enabled, so this only works with cooked builds. And you can then use this same feature to disable any game-specific server-side code. You have two options:

  1. Place #if WITH_SERVER_CODE around the bodies of individual server side functions. This is really useful if you only want to disable a function or 2 in a class that otherwise should exist on the client, but is very cumbersome if you want to hide an entire class from clients.
  2. Make a server side module. For instance on fortnite we have a FortniteServer module that has most of the GameMode classes defined in it, as well as other support code. You make a server module very similar to how you make a game-specific editor module. You’ll then have to add a conditional module dependency on your server module to your *Game.build.cs file. Here’s the relevant lines from ours, which is right after you declare the normal public and private module depdendencies:

if (UEBuildConfiguration.bWithServerCode == true)		
{
	PublicDependencyModuleNames.Add("FortniteServer");
	CircularlyReferencedDependentModules.Add("FortniteServer");
}

This will link in the module, but only when it’s not a Client configuration. Then you can use those classes from your main game module, as long as you wrap the usage in WITH_SERVER_CODE. We did this by adding this to our game pch file:


#if WITH_SERVER_CODE
#include "FortniteServerClasses.h"
#endif // WITH_SERVER_CODE


Then if we want to use a server class, we wrap the usage in WITH_SERVER_CODE as well.

3 Likes

Thanks for this info Ben!

:slight_smile:

Rama

Thanks a lot for all that info Ben! that is exactly the kind of thing i was looking for

More then likely a silly/dumb question :confused: but is there any information how how i can go about creating the second module to go along with my current project?, the docs seem to be down for me ( i allways get 404 errors ), and once again thank you for the help in solveing my issue

After some digging through Shootergame example, i found a example with the loading screen extra module it uses, so that and the information from Ben has solved my issues, so thank you a lot Ben! the information has finally allowed me to push on forward in my game.