Search Unity

Is using a networkVariable.Value bad?

Discussion in 'Multiplayer' started by Gazzzah, Nov 3, 2022.

  1. Gazzzah

    Gazzzah

    Joined:
    Jul 18, 2018
    Posts:
    16
    I'm new to networking and trying to wrap my head around it all. Could do with some clarification. I gather that the aim of the game is to reduce bandwidth as much as possible. So I tried to be clever but it's proving pretty confusing.

    To use one example in my game, players can choose the color of their character (along with other customization choices).

    This choice is represented by a uShort that acts as the index in a list of colors. Cool!

    So I wrote a combo of ServerRpc and ClientRpc to send one's color choice to all clients and apply said color. But this means that clients cannot see this data for other clients that were there first. No good.

    Ok so next I tried using a NetworkVariable and then using OnNetworkSpawn to apply the color using said NetworkVariable.

    However, what I can't seem to find anywhere is the answer to the question: Have I just created a situation where this one uShort is transmitted EVERY tick to stay in sync, even though it's only ever changed once and only ever read when someone joins. I like to imagine that the actual data transition only happens when I actually call NetworkVariable.Value but I haven't been able to confirm this.

    Additionally I see a LOT of examples where people are subscribing to the event NetworkVariable.OnValueChanged instead of calling NetworkVariable.Value which makes me feel like there's something I'm missing and ".Value bad" somehow.

    Can someone help me fill the gaps?
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Network variable values won't be constantly sync'ed, it isn't necessary as typically only the server can change the network variable value so the client will match the server.

    OnValueChanged is useful when you want to take an action when the value changes. For example if you have a network variable for player health should that value change then in the OnValueChanged event you can update that value on the UI.
     
  3. Gazzzah

    Gazzzah

    Joined:
    Jul 18, 2018
    Posts:
    16
    Ok, that does help me clear things up. Thank you