Search Unity

[MLAPI] NetworkVariable with OwnerOnly WritePermissions receives value changes from server?

Discussion in 'Netcode for GameObjects' started by TimHeijden2, May 14, 2021.

  1. TimHeijden2

    TimHeijden2

    Joined:
    Aug 11, 2016
    Posts:
    86
    The NetworkVariable has a setting for WritePermissions, which you can set to OwnerOnly. I expected this would ensure the owner client would also be the only one changing its value and raising OnValueChanged. To my surprise, in testing I found that the following occurs:

    1. Client with owner object changes a value from 0 to 1, raises OnValueChanged
    2. Client sends this data to server
    3. Server sends this data to all clients
    4. Owning client receives value and raises OnValueChanged a second time

    This becomes a problem when a variable is changed more often than the round trip time, because the client may adapt to out-of-date information that comes in from the server.

    An example of what I would expect to happen can be seen in the NetworkTransform, which solves this by using InvokeClientRpcOnEveryoneExcept so that the server will not send the data to the owning client.

    Is there a reason why NetworkVariable doesn't do this?
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    The core reason for this is as it stands today is because the server might not approve the change you did. This way you simply send a request to change the value to the server, the server then sends it down to everyone if it's valid.
     
  3. TimHeijden2

    TimHeijden2

    Joined:
    Aug 11, 2016
    Posts:
    86
    Thanks for the quick response!

    I'm not sure I understand what you mean with with the server not approving the change though.

    Since the variable is set to OwnerOnly, wouldn't it be the owner that inherently approves the change? In which scenario's would the server decide not to approve the change, based on what information? (resulting in server sending data that doesn't match what the owner sent) Doesn't that make OwnerOnly variables unreliable?

    One thought I had was to create a custom NetworkVariable class which would simply not respond to what non-owners are sending, but if such a non-approval can occur that may be a very bad idea.
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Say for example you have a synced position as a NetworkVar with client authoritative movement. If you sent a move, you could add server checks to make sure you didn't move through a wall for example, and if you did you could revert the change basically. So the new "invalid" change only ever got applied to you locally, but later reverted but never applied on remotes.

    This might get changed in the future, and I do understand your confusion. But it is how it's designed today.
     
    Last edited: May 17, 2021
    TimHeijden2 likes this.