Dedicated server check

Hi there,

I would like to know if this is the intended behavior that I am getting, or is it just me doing something wrong. What I want to do is to execute a server side logic in scene actors. Both actors check if it is the dedicated server running with:

GetWorld()->GetNetMode() == NM_DedicatedServer

I have the following setup:

  • Editor running with the Run Dedicated Server checkbox
  • 2 Actors that have server specific logic

Actor 1 is placed in the scene and does its server side logic in PreInitializeComponents().
Actor 2 is spawned via Blueprints and Initialized via Blueprint function which has server logic on Begin play

When I play the level in the editor, I am getting the following results:

  1. Actor1 Server Check - not dedicated
  2. Actor2 Server Check - dedicated
  3. Actor1 Server Check - not dedicated
  4. Actor2 Server Check - not dedicated

I looked into the implmentation of the GetNetMode() and it turned out that if no NetDriver is set, the function defaults to

return NM_Standalone;

After this, I have set a breakpoint everywhere where the NetDriver is set and the result was this:

  1. Actor1 Server Check - not dedicated
  2. UWorld::Listen - Set NetDriver
  3. Actor2 Server Check - dedicated
  4. Actor1 Server Check - not dedicated
  5. Actor2 Server Check - not dedicated

Now, this seems like a race condition and I am not really sure what is the best way to deal with it.

Have you tried IsRunningDedicatedServer()?
I would also not recommend testing this kind of logic within the editor. You’re better off building a separate executables of server and client and checking things this way.

Hi IanBreeg, thanks for the reply. Yes, I have tried it and it only works when a “real” dedicated server is running. I think it was using a compile time define to check whether the server is dedicated. The thing is, I am developing a plugin that other people would use, and I don’t want everything to blow up, when it is used incorrectly, thus the need to cover all variants.

To check if world is run as dedicated server from Editor use GameInstance->WorldContext->RunAsDedicated variable.