Search Unity

RPCMode.Server

Discussion in 'Multiplayer' started by Bugfoot, Jan 31, 2009.

  1. Bugfoot

    Bugfoot

    Joined:
    Jan 9, 2009
    Posts:
    533
    So I ran a little test to look into how Unity behaves when you send a RPC from the server to itself, using RPCMode.Server.

    My code:
    Code (csharp):
    1.  
    2. <On Click Gui Button:>
    3. networkView.RPC( "MyTestRPC", RPCMode.Server );
    4.  
    5. @RPC
    6. function MyTestRPC()
    7. {
    8.    Debug.Log( "Received test RPC" );
    9. }
    What I expected: the Unity networking layer detects that I'm trying to send an RPC to myself and simply calls the function locally, printing "Receiving test RPC" in the console

    What I observed: I get a message "Sent RPC call 'MyTestRPC' to all connected clients" in the console but it's not followed by the "Received test RPC" debug print

    If I connect a client and trigger the same RPC call, it works fine and I get the debug message on the server instance. When I try to trigger it from the server itself, it still doesn't print the debug message to itself.


    As far as I can tell, I have everything setup correctly. Is this behavior by design? If so, it's quite annoying because it forces me to make special case when sending RPCs with RPCMode.Server from the player who acts as a listen server instead of a regular client. ie:

    Code (csharp):
    1.  
    2. if( Network.isServer )
    3.    MyTestRPC()
    4. else
    5.    networkView.RPC( "MyTestRPC", RPCMode.Server );
    6.  
    Is there any way around this? Or am I doing something wrong?
     
  2. ProtonOne

    ProtonOne

    Joined:
    Mar 8, 2008
    Posts:
    406
    Unfortunately you are not doing anything wrong.

    I ran into this a while back and had to write a special case for it too.

    Another thing you may run into is that you can not send from client to client. This is hard to detect when you are just testing with 2 computers since the 2 can always send to eachother. But when you add that 3rd computer on, you would notice it.
     
  3. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    Yes this is exactly right and I too had to do this work around. The good thing is the following works:

    Code (csharp):
    1.  
    2. if( Network.isServer )
    3. playerenteredgameroomRPC(playerone);
    4. else
    5. networkView.RPC ("playerenteredgameroomRPC", playerone);
    6.  
    That is you can call and RPC function locally.

    I am going to guess that the lack of a call to the server from the server with RPCMode.Server is an intended behavior.