Search Unity

ClienRPC from Server to clients and to itself included.

Discussion in 'Multiplayer' started by MaZy, Aug 24, 2015.

  1. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    Hello I am upgrading my old network to the new unet.

    The server is authorative server and doesn't use any networkmanager. (What I mean is the input of a player goes to server, and server moves the character and send the input back to ALL players)
    Right now it is working. I can listen the server.

    Now I have problem at the character controlling part. Since it is an authorative server, the server owns all objects.

    So I spawn it via NetworkServer.Spawn ( Client part is not finished ).
    Now I have these function

    Code (CSharp):
    1.     [ClientRpc]
    2.     void RpcTest()
    3.     {
    4.         Debug.Log("Test");
    5.     }
    6.  
    7.     [ClientRpc]
    8.     void RpcOnMovementCalled(Vector3 receivedMoveVelocity) {
    9.      
    10.         networkReceiveMoveVelocity.x = Mathf.Clamp(receivedMoveVelocity.x, -1, 1);
    11.         networkReceiveMoveVelocity.y = Mathf.Clamp(receivedMoveVelocity.y, -1, 1);
    12.      
    13.         if (nIdentity.isServer)
    14.         {
    15.             // we tell to clients they should update the position of the player first.
    16.             RpcOnSyncPosCalled(transform.position, true);
    17.         }
    18.     }
    Both doesn't get fired on the server. Example if I try to execute RpcTest() it doesn't print anything in the console. How can I do it? I don't want make duplicated functions. Are there alternates? ( Let us first just ignore RpcOnMovementCalled() )
     
  2. MaZy

    MaZy

    Joined:
    Jun 29, 2012
    Posts:
    105
    No solution for me or did you not understand me?
    Just need something like RPCMode.All in UNet
     
    Last edited: Aug 27, 2015
  3. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    I don't know if there is something similar to RPCMode.All, but as a simple workaround you could just use the ClientRPC to forward a function call.
    For instance, you could create a usual function which includes the stuff you want to execute on both server and clients.
    On your server you just call this function so you have it executed on the server.
    In addition, on the server, you can call a ClientRPC. Within the ClientRPC function you just include a function call pointing to the same function you have executed on the server before. As a result, the function is executed on both the server and all clients. However, of course, the execution on the clients will be a bit later due to the ClientRPC transmission delay. You could also pass a bool parameter to the function in order to detect if it was called by the server or by the ClientRPC.
    Code (CSharp):
    1. // On server
    2. DoSomething(true);
    3. RpcDoSomething();
    4.  
    5. void DoSomething(bool fromServer)
    6. {
    7.   // put some code here
    8. }
    9.  
    10. [ClientRpc]
    11. void RpcDoSomething()
    12. {
    13.   DoSomething(false);
    14. }
     
    Last edited: Sep 2, 2015