Search Unity

New guy to networking, is there other networking API available

Discussion in 'Multiplayer' started by pierre0158, Jun 14, 2017.

  1. pierre0158

    pierre0158

    Joined:
    Jun 2, 2017
    Posts:
    5
    Hey guys,

    I used to do some networking on garry's mod, and i really don't understand all this RPC and Command stuff on Unity, is there something like


    Code (csharp):
    1.  
    2. net.StartMessage("UniqueMessage");
    3. net.WriteInt8(some_integer);
    4. net.WriteGameObject(this);
    5. net.SendToServer();
    6.  

    and something on the server side :

    Code (csharp):
    1.  
    2. net.HookNetMessage("UniqueMessage", void func);
    3.  
     
    Last edited: Jun 14, 2017
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Let me try to explain Command And RPC.
    A method marked with Command:
    Everything inside this method is Server code and only gets executed on the server. A command can be requested to be run by client.
    So if you are a client and call a command method. You basically send a packet to the server saying "Run YOUR equivilant of this method".
    If the server then wants to send something to the other clients. Thats a RPC.
    Let me show an example
    Code (CSharp):
    1. void Start()
    2. {
    3.     if(isLocalPlayer)
    4.     Cmd_ServerCode();
    5. }
    6.  
    7.  
    8. void Cmd_ServerCode()
    9. {
    10.     Rpc_ClientCode();
    11. }
    12.  
    13. void Rpc_ClientCode()
    14. {
    15.     Debug.Log("Ran on every client");
    16. }
    So if you for example want to pick up an item. You send a Command to pick it up. And in that command you can then verify. Is he looking at it? Raycast etc so the player cant modify. Please note commands can only be called if you are the local player and have authority of the object you are calling it.


    Hope I clearified a bit more.
    - TwoTen
     
    pierre0158 and Deleted User like this.
  3. pierre0158

    pierre0158

    Joined:
    Jun 2, 2017
    Posts:
    5
    Thank you, i managed to make it work !