Search Unity

NetworkVariable update before ClientRpc

Discussion in 'Netcode for GameObjects' started by _Valerio_, Sep 12, 2022.

  1. _Valerio_

    _Valerio_

    Joined:
    Apr 27, 2016
    Posts:
    9
    I have code like this:
    Code (CSharp):
    1. NetworkVariable <...>_var = new()
    2. [ClientRpc]
    3. private void F(){}
    4.  
    5. void somwhereOnServer(){
    6.    _var.Value = somevalue;
    7.    F();
    8. }
    So, function F() executed on the client before then _var variable updated on the same client. On the client/host it's updates as well. How can I fix the problem?
     
    Last edited: Sep 12, 2022
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    I could be wrong but I think the RPC is called immediately while the network variable update isn't sent to the client until the next server tick.
     
  3. _Valerio_

    _Valerio_

    Joined:
    Apr 27, 2016
    Posts:
    9
    That’s absolutely crazy, every time you using rpc you can’t trust NetworkVariable. Also you cannot trust NetworkVariable is the same on different clients(practically proven). Another useless unitymade tool…
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Network variables should be used for states and rpc's for sending transient values. I'd recommend keeping them separate although maybe rpc's will always arrive first, I've not tested it.

    Network variables should match on all clients, or at least will eventually once the updates has been received. It's worth keeping in mind updating multiple network variables on a particular object won't necessarily update in the order they're changed on the server/host.
     
  5. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    111
    It seems like its not in the dokumentation anymore OR it got changed. But it was once stated , in the documentation, that NetworkVariables can Sync on each Client differently. So if you change your Variable on Tick 1, Client 1 might geht the message on Tick 2 while Client 2 might get the message on Tick 3.
    I remember reading it somewhere in the dokumentation.

    A NetworkVariable is not a tool you use to replace RPCMessages, its more, like cerestorm mentioned, a tool to listen to a certain state that doesnt need global synchronisation
     
  6. _Valerio_

    _Valerio_

    Joined:
    Apr 27, 2016
    Posts:
    9