Search Unity

Are unassigned fields on MessageBase sent?

Discussion in 'Multiplayer' started by gsus725, Sep 4, 2017.

  1. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    Code (JavaScript):
    1. class StatMessage extends MessageBase{
    2.     var health : byte;
    3.     var stamina : byte;
    4.     var strength : byte;
    5.     var speed : byte;
    6.     var reflex : byte;
    7.     var regen : byte;
    8.     var anger : byte;
    9. }
    if I send StatMessage across the network but I only assigned the health variable like so:

    Code (JavaScript):
    1. var msg = new StatMessage();
    2. msg.health = 100;
    Am I going to experience extra network data for every field that I didn't fill out?
    For example if health is the only stat that needs updated I would only fill out that field.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yep, looks like it. You will pay the cost regardless unless using syncvars and Unet.
     
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Yes, because you're making a class with a bunch of variables in it, and the compiler is going to give them default values.

    You could investigate making them nullable, but that has overheads of its own and may end up costing more than it's worth.

    I'm no networking expert, but... for what it's worth, depending on how you're transmitting that, 7 bytes plus a little object overhead might not be worth optimising at all. Even putting aside questions of where to best focus your own effort, that may already be far smaller than a network packet (or a cache line, for that matter).
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    At this point like angry says, it's going to be the 40+byte packet overhead that's the problem (frequency is usually the biggest offender).
     
    angrypenguin likes this.
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can always write a custom serialization/deserialization, and supply a bitmask for what's changed and what's not. With 32-bit mask it would be 4+ byte overhead instead of 40. Though it would be additional overhead when all properties are used.

    Though I must say that premature optimization is the root of all evil. Make sure you're not polishing silver when the house is on fire.
     
  6. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Considering that all 7 bytes of info are likely to fit in a CPU cache line, is micro-optimisation going to have any practical effect? I would have thought that, with data structures that small, checking a bitfield to see which bytes to copy might actually end up being slower than just copying all 7 bytes regardless.

    That said, I don't know if such considerations apply in a language as high-level as C#.
     
  7. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Well. Doing such optimizations might not be for the cpu time improvement. But rather like in our example where our dedicated servers have to pay for bandwidth. We don't want to send rotation 20 times a second if only the x rotatiton has changed.

    Same kindoff applies for relay. It's not cheap.
     
  8. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    We just implemented something like what @VergilUa describes for our Smooth Sync asset and it works like a charm :)

    Now when syncing an object's state we don't have to send position if only rotation has changed and we still get the benefit of the state always being sent in one message (less bandwidth overhead than splitting them into separate messages)