Search Unity

RPC call wont function on Local Client

Discussion in 'Multiplayer' started by powdered_swords, Jun 13, 2015.

  1. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    So I was under the impression that methods marked [clientRpc] would run on the Local Client, however I'm currently only receiving calls on Remote Clients when hosting a game.

    I've attached the source code for my project below. Currently it's just two classes;

    network.cs which handles the connection set up by NetworkManagerHUD. It also spawns the Manager obj.

    test.cs is attached to the manager prefab. It checks whether all the connections are ready before initiating the RPC call that starts the game

    Thanks in advance to anyone who has any idea what I'm doing wrong.
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    try upgrading to 5.1p1
     
  3. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    What's 5.1p1? I'm currently running 5.1.0f3 which is the latest version of Unity available. Is that the same
    5.1.0 b3?
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    powdered_swords likes this.
  5. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    Did upgrading work for you? I'm using 5.1.0p1 and my [ClientRPC] methods are not being called on local clients, only remote.
     
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Your code calls the ClientRpc function when the manager object is created. The client is not even connected to the server at that point in time, so it is not sent the rpc call.

    I you want to have state that is sent to clients when they connect, use a SyncVar.
     
  7. peacefuellant

    peacefuellant

    Joined:
    Jun 18, 2015
    Posts:
    1
    I have a project based on the Roll a Ball tutorial. It's basically the same as Roll a Ball, but multiple players can join a server together to pick up the widgets. I moved the score count updating to an RPC, but the RPC only gets called on the remote clients, not the host. Here is a pastebin of the extremely simple class responsible: http://pastebin.com/SQrj0gVi

    Note that AddToCount() is only called on the server. If I add a call to SetCountText() after the RPC call, the game functions correctly.

    I read this thread and the suggestion to try 5.1.0p1, but it didn't fix the issue. Any advice is appreciated.
     
  8. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    I raised this as a bug yesterday - the response was that it was indeed a bug in 5.1.0p1 - it will be fixed in 5.2.

    For now, in case anyone finds it useful, I'm using a coroutine that runs every 100ms on the client that polls syncvars - the server changes the syncvars flags and when the client polls them it sees something's changed and reacts accordingly.
     
    peacefuellant likes this.
  9. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    You would be better off using a SyncVar hook function for that.

    Code (csharp):
    1.  
    2. SyncVar(hook=OnChange)]
    3. int myVar;
    4.  
    5. void OnChange(int newValue)
    6. {
    7.     // do stuff
    8.     myVar = newValue;
    9. }
    10.  
     
  10. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    Good idea - I've started using this. I've don't understood why the hook method takes a value though. When I tried it on my client, the SyncVar value was already updated when the hook was called...
     
  11. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    When there is a SyncVar hook, the value is not set automatically at all. You must apply the new value yourself...
     
  12. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    Ah thanks - I must have been seeing the variable already updated when I was looking at a local client... I tried it with a "proper" client and it behaved as you describe.

    It would make more sense for the SyncVar to already be updated at the time the hook is called, and the input parameter to be the old value - that way the behavior is the same on local clients and remote clients wrt to the state changes.
     
  13. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    I assume that hooks are mostly for cases, where you want to sync something with your own logic. It would be bad if you'll get the old value and the new one is already set before you could handle it
     
  14. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    yes. you want access to the old value and new value in the hook function so you can calculate a diff if required.
     
  15. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    That's right. However if you're in a local client script (ie a player running on the server), even if you are using a hook, the value will already be set when the hook is called and you never get the old value.

    That's why I suggested it would be better to get the old value as the input parameter - in the (IMHO much rarer) cases where you don't want to set the SyncVar, it could be changed back in the hook. But it was just a thought...
     
  16. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    that appears to be a bug. Filed 706433 for this.
     
    n1gth likes this.
  17. n1gth

    n1gth

    Joined:
    May 6, 2015
    Posts:
    16
    How would this be fixed? Fixing it by unsetting the variable could be confusing. As far as I understand, when a player object is running locally with the server, there's only one instance of that object, which is then used in both client and server contexts. The server mutates the syncvar - so it's natural that it should change on the client side... or is the bug that there's only one instance of the object on the server, there should be a client instance and a server instance?
     
  18. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    there is an internal property setter function that can catch the change before it is applied and call the syncvar hook.
     
    n1gth likes this.
  19. quitebuttery

    quitebuttery

    Joined:
    Mar 12, 2011
    Posts:
    329
    Was this actually fixed in 5.2? This still happens for me. RPCs do not get called on LocalClient.
     
  20. Kirbyrawr

    Kirbyrawr

    Joined:
    Jul 23, 2012
    Posts:
    945
    This is still not fixed, 5.2.1p1, the remotes are getting the call but not the server (I'm using RpcClient attribute and method)
     
  21. Kirbyrawr

    Kirbyrawr

    Joined:
    Jul 23, 2012
    Posts:
    945
    Bump! We really need to get more info about this, maybe is not a bug and there is another way to do it, we are stuck at the moment..