Multiplayer API

advanced scripting

Voxel 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:

  • SetChunkRawData applies 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 includeVoxelProperties and includeMicroVoxels values 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. Set serverMode before initialization; the scene does not need a camera. World generation, edits, colliders and physics queries all work without rendering. 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 in server mode, which is the signal to use when you need to know that a chunk's collider is in place. Note that rendering resources such as materials and texture arrays are still created even in server mode.

Take a look at other methods that let you get or set voxels as well here.

Was this page helpful?