Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Unity Multiplayer Using #if to exclude server specific code

Discussion in 'Connected Games' started by Demozo, Jul 28, 2015.

  1. Demozo

    Demozo

    Joined:
    Aug 19, 2014
    Posts:
    20
    Is this a feasible way of not having the client know how the server does things?

    Imagine the following:
    Code (CSharp):
    1. [Command]
    2. private void CommandDoServerOnlyStuff()
    3. {
    4.     #if SERVER
    5.     // Server code
    6.     #endif
    7. }
    The server would have the actual implementation, but the client would only know about its existence.
    Would this mess with the way uNet works?

    Assuming the way I understand commands work is correct, which is the client just lets the server know it wants to run this method, this should have no noticeable impact other than that the client doesn't include server specific code in the first place.
     
  2. Disastorm

    Disastorm

    Joined:
    Jun 16, 2013
    Posts:
    69
    I was doing something similar ( except I was doing if(this.isServer) , didn't know you could do #if SERVER ).
    However, I later found out that [Command] only actually run on the server, the client never runs them. I actually added Log statements to test it out and find out exactly how it works since the Docs don't explicitly tell you.

    Calling a "Command" on the client, will execute it on the server only.
    Calling a "ClientRpc" on the server will execute it on the client only.

    Also, unless I"m mistaken the "Command" functions must be prefixed with "Cmd" not "Command".

    However, outside of Commands, and ClientRpcs, I see nothing wrong with using if statements, perhaps others may have other ideas.
     
  3. jpthek9

    jpthek9

    Joined:
    Nov 28, 2013
    Posts:
    944
    #if statements are messages to the compiler to compile the code within the block if a certain condition is met. You'd have to compile and build twice, once for the server and once for your clients.
     
  4. Demozo

    Demozo

    Joined:
    Aug 19, 2014
    Posts:
    20
    I would have no problem doing this, I'd much rather do it that way than have server side code exist in the client.