Are unassigned fields on MessageBase sent?

class StatMessage extends MessageBase{
    var health : byte;
    var stamina : byte;
    var strength : byte;
    var speed : byte;
    var reflex : byte;
    var regen : byte;
    var anger : byte;
}

if I send StatMessage across the network but I only assigned the health variable like so:

var msg = new StatMessage();
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.

Yep, looks like it. You will pay the cost regardless unless using syncvars and Unet.

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).

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).

1 Like

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.

3 Likes

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#.

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.

3 Likes

We just implemented something like what @VergilUa describes for our Smooth Sync asset and it works like a charm :slight_smile:

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)