Search Unity

Question Pass a NetworkVariable to a Client RPC

Discussion in 'Netcode for GameObjects' started by BSimonSweet, Oct 12, 2022.

  1. BSimonSweet

    BSimonSweet

    Joined:
    Aug 17, 2022
    Posts:
    67
    Hello,

    I'm hitting a limit of N4GO, which is that updating a NetworkVariable before calling a Client RPC won't ensure that this NetworkVariable will be updated on client-side.

    Code (CSharp):
    1. private NetworkVariable<int> myValue = new();
    2.  
    3. [ClientRPC]
    4. public void DisplayMyValue_ClientRPC()
    5. {
    6.     /** On client */
    7.    
    8.     // Here, I expect that myValue have the latest value
    9.    
    10.     Debug.Log(myValue.Value);
    11. }
    12.  
    13. private void DoStuffOnServer()
    14. {
    15.     /** On server */
    16.  
    17.     // ...
    18.  
    19.     myValue.Value = Random.Range(0, 1000);
    20.     DisplayMyValue_ClientRPC();
    21. }
    In practice, DisplayMyValue_ClientRPC() will be called before myValue is updated, on client. In a perfect world, the order of requests should be preserved when received on a client, but I don't know if that something possible.

    But, on the Netcode documentation, this case seems to be covered : https://docs-multiplayer.unity3d.co...r#why-not-use-networkvariables-for-everything (in the TIP section)

    It says that passing the NetworkVariable to the RPC will ensure that it is up-to-date when the RPC is called on client. But ... it is not possible to do that, it gives a compilation error. Which is correct because N4GO doesn't know how to serialize NetworkVariable ...

    I don't understand why the doc states that we can do that, when we can't. Is this section up-to-date ?

    (on another note, the GitHub links of the examples are broken)

    Thank for your help !
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I am not sure but you may be able to pass the network variable using RPC params. Anyway, you can wait in the client RPC until OnValueChanged has been called
     
  3. BSimonSweet

    BSimonSweet

    Joined:
    Aug 17, 2022
    Posts:
    67
    It is not possible with RPC params. I'll will use OnValueChanged, but it will make the code a callback-hell in the long run ...
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    As you've discovered ordering of network variable updates isn't maintained when sent to client, neither with rpc's nor other network variable updates. If you need multiple values for a particular event on the client it's best to pass them all in a rpc.

    That documentation doesn't make sense to me either, it's suggesting a client rpc can be used to update network variables which won't work, at least not how it's shown there.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    I would assume they don't mean to send NetworkVariable<int> with the RPC but just the current int value.
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    This is the part I have a problem with:
    upload_2022-10-12_20-31-45.png

    It's implying the network variables can be set via the client rpc but as it's run client-side it won't work. It can be made to work but that needs an explanation which is outside the scope of the document.