Code Generation
So for this script:
using Mirage;
public class Data : NetworkBehaviour
{
[SyncVar(hook = nameof(OnInt1Changed))]
public int int1 = 66;
[SyncVar]
public int int2 = 23487;
[SyncVar]
public string MyString = "Example string";
void OnInt1Changed(int oldValue, int newValue)
{
// do something here
}
}
The following sample shows the code that is generated by Mirage for the SerializeSyncVars
function which is called inside NetworkBehaviour.OnSerialize
:
public override bool SerializeSyncVars(NetworkWriter writer, bool initialState)
{
// Write any SyncVars in base class
bool written = base.SerializeSyncVars(writer, forceAll);
if (initialState)
{
// The first time a game object is sent to a client, send all the data (and no dirty bits)
writer.WritePackedUInt32((uint)this.int1);
writer.WritePackedUInt32((uint)this.int2);
writer.Write(this.MyString);
return true;
}
else
{
// Writes which SyncVars have changed
writer.WritePackedUInt64(base.syncVarDirtyBits);
if ((base.get_syncVarDirtyBits() & 1u) != 0u)
{
writer.WritePackedUInt32((uint)this.int1);
written = true;
}
if ((base.get_syncVarDirtyBits() & 2u) != 0u)
{
writer.WritePackedUInt32((uint)this.int2);
written = true;
}
if ((base.get_syncVarDirtyBits() & 4u) != 0u)
{
writer.Write(this.MyString);
written = true;
}
return written;
}
}
The following sample shows the code that is generated by Mirage for the DeserializeSyncVars
function which is called inside NetworkBehaviour.OnDeserialize
:
public override void DeserializeSyncVars(NetworkReader reader, bool initialState)
{
// Read any SyncVars in base class
base.DeserializeSyncVars(reader, initialState);
if (initialState)
{
// The first time a game object is sent to a client, read all the data (and no dirty bits)
int oldInt1 = this.int1;
this.int1 = (int)reader.ReadPackedUInt32();
// if old and new values are not equal, call hook
if (!base.SyncVarEqual<int>(num, ref this.int1))
{
this.OnInt1Changed(num, this.int1);
}
this.int2 = (int)reader.ReadPackedUInt32();
this.MyString = reader.ReadString();
return;
}
int dirtySyncVars = (int)reader.ReadPackedUInt32();
// is 1st SyncVar dirty
if ((dirtySyncVars & 1) != 0)
{
int oldInt1 = this.int1;
this.int1 = (int)reader.ReadPackedUInt32();
// if old and new values are not equal, call hook
if (!base.SyncVarEqual<int>(num, ref this.int1))
{
this.OnInt1Changed(num, this.int1);
}
}
// is 2nd SyncVar dirty
if ((dirtySyncVars & 2) != 0)
{
this.int2 = (int)reader.ReadPackedUInt32();
}
// is 3rd SyncVar dirty
if ((dirtySyncVars & 4) != 0)
{
this.MyString = reader.ReadString();
}
}
If a NetworkBehaviour has a base class that also has serialization functions, the base class functions should also be called.