Custom Snapshot Data
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.
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.
Writing Snapshot Data
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
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 )
{
// ...
}
Referenced API
Canonical API pages mentioned in this guide.
Context for reading binary blob data. Wraps ByteStream for allocation-free deserialization.
Context for writing binary blob data. Wraps ByteStream for allocation-free serialization.
Write and read bytes to a stream. This aims to be as allocation free as possible while also being as fast as possible.
A GameObject can have many components, which are the building blocks of the game.
When implemented on a `Sandbox.Component` or `Sandbox.GameObjectSystem` it can read and write data to and from a network snapshot.
A variable unit based length. ie, could be a percentage or a pixel length. This is commonly used to express the size of things in UI space, usually coming from style sheets.
Read data from the snapshot.
Write data to the snapshot.