Search Unity

Question Is this method to hide server code good practice?

Discussion in 'Netcode for GameObjects' started by Dallion3K, Jun 19, 2021.

  1. Dallion3K

    Dallion3K

    Joined:
    Dec 14, 2018
    Posts:
    4
    I'm trying to develop a game utilising the MLAPI and am testing a (scrappy) method to hide server code from the client.
    I have made a custom compiler define called UNITY_SERVER.
    I have built a client with UNITY_SERVER not defined, and a server with it defined. All the application has is a plane, a player prefab, a network manager, and a small script to display a "Start Server" and a "Start Client" button in the GUI.
    I start the server, I start the client, I press up and hey presto the player moves up.

    I just want to confirm whether this is good practice (probs not) and whether building the client application automatically hides server code or not without the need for custom defines?

    Edit: after further testing the methods do not actually get stripped even with HIGH stripping level and set to Release.
    Edit #2: I am aware of the server build option in the build settings, I mostly would like to know whether server side code, specifically ServerRPC's are stripped in the client version.

    Code (CSharp):
    1. using MLAPI;
    2. using MLAPI.Messaging;
    3. using MLAPI.NetworkVariable;
    4. using UnityEngine;
    5. public class TestPlayer: NetworkBehaviour
    6. {
    7.     NetworkVariableVector3 POS = new NetworkVariableVector3(Vector3.zero);
    8.     void Update()
    9.     {
    10.         if (IsClient)
    11.             transform.position = POS.Value;
    12.     }
    13.     public void MoveUp()
    14.     {
    15.         MoveUpServerRPC();
    16.     }
    17.  
    18.     [ServerRpc]
    19.     public void MoveUpServerRPC()
    20.     {
    21. #if UNITY_SERVER
    22.         POS.Value = POS.Value + Vector3.forward;
    23. #endif
    24.     }
    25.  
    26. }
    This is my first post so my apologies if I missed anything and thanks in advance!
     
    Last edited: Jun 21, 2021
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Unity already has a `UNITY_SERVER` that's why defining your own does not work. UNITY_SERVER code gets stripped from the build unless you have the 'server build' checkbox enabled in build settings.

    (check server build here)
     
    Dallion3K likes this.
  3. Dallion3K

    Dallion3K

    Joined:
    Dec 14, 2018
    Posts:
    4
    Yes I am aware of that, I need to make that clear in the post that I have done that too, thank you :) . The problem is in release builds with high code stripping the ServerRPC's code still exist client side from my experiments. I'd like to do a dedicated server and client but am finding that server code still exists in the client because of the RPC's? I could be completely wrong on that and its something else since the debug builds may not do all that has been defined but I just want to be sure the MLAPI strips server code in the client build. Thanks in advance!
     
  4. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Compile defines are a separate feature from code stripping. They are always enabled even if you disable code stripping.

    MLAPI shouldn't really change how compile defines strip code, so the content of your RPCs which you strip out should not be present in client builds. So either your verification of the code being stripped is wrong (how are you checking this?) or there is a severe bug in the #UNITY_SERVER define.

    Also please make sure that you do not have a custom defined UNITY_SERVER define in your build settings. This name is already reserved for an Unity controlled define, I do not think we support overriding it with a custom define and it could cause all kinds of problems (maybe it's fine, I don't know)
     
  5. Dallion3K

    Dallion3K

    Joined:
    Dec 14, 2018
    Posts:
    4
    Cheers for the quick response. You've cleared up some of my confusion and I will double check my findings.
    In the code above there is the additional OnGUI method which simply draws the response of another ServerRPC.
    This other S'RPC was as follows:

    Code (CSharp):
    1.  
    2. [ServerRPC]
    3. bool ServerCode(){
    4. #if UNITY_SERVER
    5. return true;
    6. #endif
    7. return false;
    8. }
    9.  
    It could be down to the custom define, I included a custom one since whenever I typed it, the auto suggest wouldn't come up. The built in one it wouldn't come up, now that I know I can't use it I'll make sure to use an alternative one.
     
    Last edited: Jun 21, 2021
  6. Dallion3K

    Dallion3K

    Joined:
    Dec 14, 2018
    Posts:
    4
    Thanks again for clearing up the confusion. I have now successfully done what I intended without the need for a custom define. I am now using partial classes to split the client-side object and the server-side object, and using the built-in define UNITY_SERVER to alter things in a way which gives me more control and a better work flow.
     
    luke-unity likes this.