Detecting if running on dedicated server at runtime ?

Hi. How can I detect if a piece of C++ code is being executed on a dedicated server at runtime in the editor ? I’ve tried ‘IsRunningDedicatedServer()’ and this returns false for both server and client processes while running in the editor.

I’m trying to write some client-side only logic and I’m having real problems detecting when I’m just running as a client.

Thanks.

On any AActor derived class you can use compare GetNetMode() with NM_DedicatedServer

Thanks, is there any way to perform this check from UGameInstance constructor at game boot time ? I want to bring up certain manager objects only if running on the server. At the point I would like to do this there is no valid World object.

Wrap the code for that with **WITH_SERVER_CODE **Preprocessor flag

Example



#if WITH_SERVER_CODE

// code to load up your mananger and stuff here

#endif


in this setup the code to load manager and other things would only be present in the Server builds and hence will only run on dedicated servers

Hi, When I add that check around the code and run in the editor all the code thinks it is being run on the server… 8/

Is there any documentation on the differences between running inside the editor in the various modes versus running in standalone builds ? What’s the best strategy for testing and debugging client/server logic when inside the editor ?

Thanks

You want to use HasAuthority(). Though I am also not thrilled that IsRunningDedicatedServer() does not return true when simulating in the editor and the “Run Dedicated Server” option is checked.

GetGameInstance()->IsDedicatedServerInstance()

Haha great we have

  1. IsRunningDedicatedServer()
  2. GetNetMode() w/ NM_DedicatedServer
  3. #if WITH_SERVER_CODE
  4. HasAuthority()
  5. GetGameInstance()->IsDedicatedServerInstance()

Clear as mud!

1 Like