I’m interested in writing Serialization functionality so that I can have an object in one of my classes sync automatically. However, when trying to write an example with the example given on the state synchronization page, I can’t seem to replicate the example without compile errors. Specifically base.get_syncVarDirtyBits() doesn’t seem to exist.
Ok thanks for that. Seems to fix the compile errors.
if ((base.syncVarDirtyBits & 1u) != 0u)
{
if (!wroteSyncVar)
{
// write dirty bits if this is the first SyncVar written
writer.WritePackedUInt32(base.syncVarDirtyBits);
wroteSyncVar = true;
}
writer.WritePackedUInt32((uint)this.int1);
}
Can I get a little more explanation about the if statement. I noticed in the example, there being multiple checks with 1u/2u/4u. What are these values and where are they being calculated from and how would I work it out with an example of my own?
the constants 1u/2u/4u are bit masks that correspond to index positions into the array of dirty bits. These are used for telling the client which syncvars on the script have changed.
So:
the first sync var at position 0 uses the mask 1u.
the second sync var at position 1 uses the mask 2u.
The sync var dirty mask for the script is a binary OR of the masks of the dirty bits that are set.
The dirty bit mask is included in the update packet.
The client can then do a binary AND against that dirty mask for each position and tell which syncvars change, and only read values for those from the NetworkReader stream.