Multiplayer API
advanced scriptingVoxel Play 4 · Scripting / API
One aspect to cover in multiplayer environments is the world synchronization. You can use the event OnChunkChanged to send modifications to the server or other clients (check other events here). Then you can use the following methods to quickly gather the data of a chunk or apply that data on other clients:
public byte[] GetChunkRawBuffer()
Returns a newly allocated byte array to store contents of a chunk. Use with GetChunkRawData() method:
public int GetChunkRawData(VoxelChunk chunk, byte[] contents, bool includeVoxelProperties = true, bool includeMicroVoxels = true)
Writes the contents of a chunk (voxels) into the array contents starting at index "baseIndex". The allocation of the byte array should be obtained using GetChunkRawData() method. Contents are packed using RLE compression to reduce memory usage. The method returns the actual data length contained in contents array.
public void SetChunkRawData(VoxelChunk chunk, byte[] contents, int length, bool validate = true, bool includeVoxelProperties = true, bool includeMicroVoxels = true)
Replaces the contents of a chunk with "contents". Length param determines the length of valid data inside the contents array (because the array can be bigger than the actual data in it; . If validate is set to true, the voxel types will be checked to ensure they correspond to existing voxel definitions. The contents of the byte array should be obtained using GetChunkRawData() method.
The methods above will automatically compress/uncompress the data of the chunk using an RLE algorithm to reduce the bandwidth used when sending chunk data over the network.
What the raw format does and does not do
The payload carries voxels, and optionally voxel properties and microvoxels. It does not carry the chunk coordinates, a format version or any option flags, so framing and integrity checking belong to your networking layer. Keep these points in mind:
SetChunkRawDataapplies the data but does not rebuild the chunk. Request a chunk refresh afterwards if you want the mesh and collider to follow.- Voxel definitions must be registered identically on both ends, in the same order: what travels is the type index, not the name.
- The same
includeVoxelPropertiesandincludeMicroVoxelsvalues must be used when writing and when reading. - A truncated or malformed payload is detected and decoding stops, but the chunk is not rolled back to its previous contents. Validate packets before applying them.
Server mode
A dedicated server can run Voxel Play with rendering disabled. Enable Server Mode in the Advanced section of the
Voxel Play Environment inspector, or set serverMode by script before initialization. The scene does not need a camera, and
enabling the option also turns on Low Memory Mode.
World generation, terrain graphs, voxel edits, save games, colliders, physics queries, navmesh and events behave exactly as they do
on a client. If the area you want streamed is away from the world origin, assign a distanceAnchor so chunks are generated
around it. Keep colliders enabled if you rely on physics: OnChunkRender is raised on every completed rebuild, and it is the
signal to use when you need to know that a chunk's collider is in place. Voxel placement animations are bypassed, so an edit is
readable immediately after the call instead of on the next frames.
Rendering work is skipped: chunk meshes are never uploaded to the GPU, per-chunk visual meshes are not created, texture arrays are allocated without pixel data, voxel thumbnails are not rendered, clouds and particle pools are not built, and meshing buffers start small and grow on demand. A server therefore holds considerably less memory and generates the world faster than a client running the same scene. Custom voxels are still instantiated as GameObjects, since they usually carry gameplay logic.
Take a look at other methods that let you get or set voxels as well here.
Suggest an improvement
Help us improve this documentation page.