Scripting / VoltCS
Networking
Ownership, synced fields and network events: how players see the same world.
Every player runs their own copy of your scripts. Networking keeps those copies in agreement.
In a world with subrooms, each subroom is networked on its own: scripts only share state with the players in the same subroom, and the master is decided per subroom.
Ownership#
Every object with a networked script has an owner: one player whose copy decides its state. An object nobody claimed belongs to the instance master.
| Call | What it does |
|---|---|
IsOwner / Owner |
Whether you own this object, and who does. |
TakeOwnership() |
Makes you the owner. The current owner's OnOwnershipRequest can refuse. |
SetOwner(player) |
Hands the object to someone. Only the owner or the master can. |
VoltNetworking.IsMaster, .Master |
Whether you are the master, and who is. |
OnOwnershipTransferred runs when the owner changes, and OnMasterChanged when the master leaves and another player takes over.
Synced fields#
Mark a field [VoltSynced] and the owner's value is sent to everyone. Other players receive it and then get OnDeserialization.
[VoltSynced] private int score;
[VoltSynced(VoltSyncInterpolation.Smooth)] private float doorAngle; // eases to new values
How a behaviour sends its fields is set with [VoltSync] on the class:
| Mode | How it sends |
|---|---|
Manual (default) |
When the owner calls RequestSerialization(). For values that change now and then. |
Continuous |
Several times a second while values change. For things that move all the time. |
None |
The behaviour has no networked state. |
[VoltSync(VoltSyncMode.Continuous)]
public class SpinningPlatform : VoltBehaviour { ... }
OnPreSerialization runs on the owner just before sending, OnPostSerialization after. Players who join later receive the current values.
Use Volt > Network Cost to see how many bytes each script sends.
Network events#
A method marked [VoltNetworkEvent] can be run on other players' clients:
public VoltAudioSource fanfare;
[VoltNetworkEvent]
public void PlayFanfare()
{
fanfare.Play();
}
public override void OnInteract()
{
SendNetworkEvent(VoltNetworkTarget.All, "PlayFanfare");
}
The event name must be a constant string. A method without the attribute can never be called over the network. Events can carry up to 8 arguments.
Targets#
| Target | Who runs it |
|---|---|
All |
Everyone, the sender included. |
Others |
Everyone except the sender. |
Owner |
The owner of the object. |
Master |
The instance master. |
Authority |
The owner, or the master when there is none. |
Local |
Only this client; nothing is sent. |
AllBuffered |
Everyone now, and every player who joins later, once and in order. |
Use SendNetworkEvent(player, ...) to target one player.
Late joiners#
A player who joins mid-game gets every synced field, who owns what, held pickups, seats and object pools, even when the world takes a while to load. Video players start at the same point in the video as everyone else's.
For things that happen through events, send them buffered so later players see them too. A light switch everyone sees, including whoever joins in an hour:
[VoltNetworkEvent]
public void Toggle()
{
lamp.SetActive(!lamp.ActiveSelf);
}
public override void OnInteract()
{
SendNetworkEvent(VoltNetworkTarget.AllBuffered, nameof(Toggle));
}
OnPlayerJoined runs as soon as a player arrives, while they may still be loading. OnPlayerReady(VoltPlayer player) runs once they have loaded and caught up, so events you send them from there arrive. For the local player it runs once, when this client is caught up.
Who may run what#
| Attribute | Effect |
|---|---|
[VoltOwner] |
The method only runs on the owner. A network event marked with it is accepted only from the owner. |
[VoltServer] |
The method only runs on the instance master. A network event marked with it is accepted only from the master. |
[VoltLocal] |
On a method: only runs for the local player, never from the network. On a field: belongs to each client and is never synced. |
Use these to stop players from triggering things they should not.
Moving objects#
Add VoltObjectSync to an object to keep its position and rotation the same for everyone; the owner's copy decides. VoltPickup with a VoltObjectSync makes something players can carry. VoltObjectPool lets the master spawn and return pooled objects for everyone, late joiners included.
Saving bandwidth#
VoltNetworking.InterestDistance makes synced objects far from every other player send less often.
