Search Unity

[HELP] Using Netcode for Dedicated(Headless) Server!

Discussion in 'Netcode for GameObjects' started by Moridorn, Jan 12, 2022.

  1. Moridorn

    Moridorn

    Joined:
    Jul 28, 2016
    Posts:
    4
    Hello,

    I'm making a multiplayer game with authorative server logic using headless unity server build. I decided to use Netcode(MLAPI). I'm doing a personal project of 1v1 / 2v2 / 3v3 multiplayer arena.

    I understand that by building headless server build, Unity builds the initial scene into the project without a graphic interface and only a non-inputable console window. So I figured it's maybe best to make a ServerScene where I have NetworkManager and a few other monobehaviours that I need for the server to run. Because why build with MainMenuScene and wasting memory when all I need really is NetworkManager and initial Server script which starts server. It all works fine, by building client, the client doesn't have ServerScene in the build, so all of that code is hidden from the client build.

    What I initially understood about headless server builds is that I have server-side logic and via transport layer exposed methods or events that I can trigger from the client-side and send parameters and via that way communicate with the server. Similar to how Photon has plugins on PhotonServer and how Gamesparks has Requests that are sent from the client, and I thought RPCs are just a similar way of communicating with the Server.

    What puzzles me now is that the only way to communicate with a server is through NetworkObjects or NetworkBehaviour and they sync well, but how do I make procedure calls to server and vice versa, so I learned about RPCs, but my issue is that I can't fully hide server-side logic from client-side logic, because if I must have:

    Code (CSharp):
    1. [ServerRpc]
    2. private void CallFromClientToServerRpc() { /* ... */ }
    3.  
    4. [ClientRpc]
    5. private void CallFromServerToClientRpc() { /* ... */ }
    on client side object in the same script, then any kid can use ILSpy and decompile csharpAssembly and see server side logic. Which is a breach. But if I use #if UNITY_SERVER define, then I'll skip the code in the client build that I need to have in order for the client to be able to 'know' about the server side procedure.

    Also does one server instance, one .exe only support 1 room? What if my players in example during a prematch setup, enter a separate scene during their preparation for the arena match, and then after the setup everyone gets loaded up into the same unity scene. Is it possible to handle that?

    I'm still new to the multiplayer programming, so please any help would be greatly appreciated.

    I'm a professional game developer in the game development industry for years now, but this is my first jumping into client-server architecture and mainly multiplayer programming.
     
    Acrodyn likes this.
  2. Vadimskyi

    Vadimskyi

    Joined:
    Jan 13, 2020
    Posts:
    12
    Maybe you could write something like this:

    Code (CSharp):
    1.  
    2. class ClientServerRpcs
    3. {
    4. [ServerRpc]
    5.     void CallFromClientToServerRpc()
    6.     {
    7.         #if UNITY_CLIENT
    8.         //client logic
    9.         #endif
    10.     }
    11.  
    12. [ClientRpc]
    13.     void CallFromServerToClientRpc()
    14.     {
    15.         #if UNITY_SERVER
    16.         //server logic
    17.         #endif
    18.     }
    19. }
    20.  
    If you want to load everyone into the same scene why load them in separate during prematch?
     
    Last edited: Jan 13, 2022
  3. Moridorn

    Moridorn

    Joined:
    Jul 28, 2016
    Posts:
    4
    How does one test server? Lets say for example I want to create a server only logic, I would have a ton of scripts with #if UNITY SERVER preprocessor directives and if I want to test it, or for example debug server side of the logic, I would have to build client version of the game, and run server side in Unity, and then I would have to go to every script and put ! infront of UNITY_SERVER to enable that part of the code in testing, is there a simpler way?
     
  4. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Any script that inherits from NetworkBehaviour has the bools "IsServer", "IsClient", "IsHost" (so both server and client), "IsOwner" and more set, so these allow you to easily exclude logic.