Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ClientRpc to all clients except one client?

Discussion in 'Multiplayer' started by CloudyVR, Oct 8, 2017.

  1. CloudyVR

    CloudyVR

    Joined:
    Mar 26, 2017
    Posts:
    709
    How do I send a ClientRpc to all clients expect the client who told the server to start the Rpc?

    I have a vehicle and the players can slide a door. When the door moves the client sends a Cmd to the server with the new position.

    However once the server gets that Cmd and then dispatches the Rpc, the client moving the door now gets the Rpc and the door goes crazy because it's being told how to move from both the server and the client at almost the same exact time.

    What I need is pretty much what I've got going right now but just not to have it send an update to the one client currently moving the door.

    I have looked for a while to find a way to do this, and aside from iterating through a list of clients and calling a bunch of TargetRpc calls, I am fresh out of ideas.

    How can I accomplish this?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you don't want to iterate the list of clients for TargetRpc, I would probably do something like this.

    Code (csharp):
    1.  
    2. private bool iMovedTheDoor = false;
    3.  
    4. public void MoveTheDoorAndStuff()
    5. {
    6.     iMovedTheDoor = true;
    7.  
    8.     // Do animation here to move the door locally on the client that initiated it
    9.  
    10.     CmdMoveDoorOnServer();
    11. }
    12.  
    13. [ClientRpc]
    14. void RpcMoveDoorOnClients()
    15. {
    16.     if (iMovedTheDoor == false)
    17.     {
    18.         //Do the animation or whatever to move the door on all other clients
    19.     }
    20.     else
    21.     {
    22.         //If I get here that mean this is the client that initiated the door move, so lets just reset the door bool
    23.         iMovedTheDoor = false;
    24.     }
    25. }
    26.  
    27. [Command]
    28. void CmdMoveDoorOnServer()
    29. {
    30.     RpcMoveDoorOnClients();
    31. }
    32.  
    You may have to make this a little more complicated if you're going to allow multiple players to move the door at the same time, but this is the idea I would probably use at least. Alternatively you could send a client ID in the cmd and send it back in the rpc, and if the id is the same as this client then ignore the rpc.

    You could also consider skipping all this and not moving the door until the client receives the ClientRpc, but that would add a slight delay to the client that opens the door.
     
    Last edited: Oct 10, 2017
    llluuo_ and CloudyVR like this.
  3. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    832
    I handle this kind of stuff with local booleans in the rpc, like above.
     
    Joe-Censored and CloudyVR like this.
  4. ScetticoBlu

    ScetticoBlu

    Joined:
    Nov 26, 2020
    Posts:
    17
    Hi guys! Is there some sort of best practice between using a boolean or iterate the client list? I'm making a multiplayer game with at most 5 players in the same lobby, so I think it won't slow down too much if I iterate the client list instead of using boolean, what do you think?
    EDIT:
    I'm using mirror
     
    Last edited: Dec 6, 2020