Search Unity

Question Is there an event trigger available for "all network variables synced" ?

Discussion in 'Netcode for GameObjects' started by Lantha_, Jul 1, 2022.

  1. Lantha_

    Lantha_

    Joined:
    Aug 28, 2017
    Posts:
    8
    Hello,

    I'm making a turn-by-turn game and trying to put as few things as I can in the update function as I want it to run as fast as possible.
    To achieve that, once the server is done computing the rules, I want to do a clientRPC or en event trigger on a networkvariable to trigger the UI update with the computed values for all the clients.
    The problem is that, with either method, when it triggers on the non-host-client-side, the other networkvariables (damage done, healing done, bombs left, etc) are not all updated yet and the resulting UI doesn't make sense.
    So I thought about an event trigger to know when all variables are synced but couldn't find one in the documentation.
    Is there one I didn't find?
    Is there another way I could achieve the same result?
    Am I doing something the wrong way ?

    Thanks for any answer and have a great day y'all :D
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I've come across the same issue, where updating multiple network variables on the same object doesn't necessarily happen on a client in the same order. I was able to work around this by dealing with each variable change in isolation and not have to rely on other variable values, and have a service to handle updating the UI by single value changes.

    I would advise going down that route, there will be other options but it will likely mean moving away from using network variables.
     
    Last edited: Jul 2, 2022
  3. Lantha_

    Lantha_

    Joined:
    Aug 28, 2017
    Posts:
    8
    It's a shame that we only have a workaround yet. To do smth similar to your suggestion, I guess I could add an event listener for each variable and have them set local flags to true until they're all true and then the UI update triggers.
    But would it be faster than just checking in the update function?
    I'm still open to suggestions if anyone else has one.

    Thanks
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    You'll have to explain how you're currently updating the UI and why updating individual values would be a problem. You can use the network variable's OnValueChanged callback effectively as a trigger for the update.
     
  5. Lantha_

    Lantha_

    Joined:
    Aug 28, 2017
    Posts:
    8
    I guess I just wanted to do it all in one function so the calls are easier and because everything should happen all at the same time anyway.
    Except it shouldn't since the values don't all update at the same time.
    Gonna try to split it like you suggested :D
     
    cerestorm likes this.
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    I think the added flexibility is worth it in this case. If you do encounter a situation were you want a more 'atomic' update of multiple values, one option is to have a struct as a network variable with those values.

    An example I'm using:

    Code (CSharp):
    1.     public struct VicinityLocationDetail
    2.     {
    3.         Vector2Int position;
    4.         VicinityType vicinityType;
    5.         VicinityEntityFlags entityFlags;
    6.         uint entitySideId;
    7.         int units;
    8.  
    9.         public override string ToString()
    10.         {
    11.             StringBuilder stringBuilder = new StringBuilder("VicinityLocationDetail");
    12.             stringBuilder.Append(" Position ").Append(Position);
    13.             stringBuilder.Append(" VicinityType ").Append(VicinityType);
    14.             stringBuilder.Append(" EntityFlags ").Append(EntityFlags);
    15.             stringBuilder.Append(" EntitySideId ").Append(EntitySideId);
    16.             stringBuilder.Append(" UnitNumber ").Append(Units);
    17.  
    18.             return stringBuilder.ToString();
    19.         }
    20.  
    21.         public Vector2Int Position { get => position; set => position = value; }
    22.         public VicinityType VicinityType { get => vicinityType; set => vicinityType = value; }
    23.         public VicinityEntityFlags EntityFlags { get => entityFlags; set => entityFlags = value; }
    24.         public uint EntitySideId { get => entitySideId; set => entitySideId = value; }
    25.         public int Units { get => units; set => units = value; }
    26.     }
    Code (CSharp):
    1.     public class VicinityLocation : NetworkBehaviour
    2.     {
    3.         NetworkVariable<VicinityLocationDetail> locationDetail = new NetworkVariable<VicinityLocationDetail>();
    4.         .
    5.         .
    6.         .
    7.     }
    I'm still using version pre7, in case it needs changing to work in pre10.