In some circumstances you may want to add additional data to the network snapshot that gets sent to clients when they join a server. One example of this might be serializing and deserializing voxel world data.
Components in the Scene can write and read their own data during the snapshot sending and receiving process by implementing Component.INetworkSnapshot
.
To write snapshot data for a Component you can simply override the WriteSnapshot
method on a component.
private byte[] MyVoxelData { get; set; }
void INetworkSnapshot.WriteSnapshot( ref ByteStream writer )
{
writer.Write( MyVoxelData.Length );
writer.WriteArray( MyVoxelData );
}
Reading snapshot data can be done by overriding ReadSnapshot
on a component. You can return a Task here to have the loading screen wait for this to be done before continuing.
void INetworkSnapshot.ReadSnapshot( ref ByteStream reader )
{
var length = reader.Read<int>();
MyVoxelData = reader.ReadArray<byte>( length ).ToArray();
}
protected override Task OnLoad()
{
await LoadVoxelWorld( MyVoxelData );
}
private Task LoadVoxelWorld( byte[] data )
{
// ...
}