Search Unity

Bug NetworkBehaviour NetworkVariable OnValueChanged bug or by design?

Discussion in 'Netcode for GameObjects' started by kakhao, Jul 18, 2021.

  1. kakhao

    kakhao

    Joined:
    Jan 1, 2020
    Posts:
    4
    Strange thing, NetworkVariable fires OnValueChanged twice only on client if it IsOwner.
    First time we have real update with diferent previous and new values and second time both parameters are the same. Is it bug?

    BTW, maybe for someone it would be useful, NetworkBehaviour behaviour :
    If host:
    1. Awake()
    2. OnEnable()
    3. NetworkStart()
    4. Start()
    On Clients Side we have
    1. Awake() for Remote NetworkObject;
    2. OnEnable() for Remote
    3. NetworkVariable OnValueChanged Remote if one registered before - we receive fresh value
    4. NetworkStart() for Remote
    5. Awake() for Local
    6. OnEnable for Local
    7. NetworkVariable OnValueChanged Local - we receive default value regardless no changes done before;
    8. NetworkStart() for Local
    9. Start for Remote
    10. Start for Local (here we set own NetworkVariable)
    11. NetworkVariable OnValueChanged Local - we receive updated value
    12. NetworkVariable OnValueChanged Local - we receive updated value again regardless no changes
     
  2. kakhao

    kakhao

    Joined:
    Jan 1, 2020
    Posts:
    4
    Discovered this case, seems we have scenario:
    1. Client sets the NetworkVariable and this way fires ValueChanged event;
    2. NetworkManager sends new value of the variable to server, server updates the variable and send to all clients;
    3. Clients call NetworkVariable.ReadDelta and now it doesn't check if variable changed, just fires event.
     
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306