Networking
How to make a Basis world or prop behave the same for every player, synced variables, network events, ownership and late joiners.
Everything a script does runs on one player's machine. If a player opens a door, nobody else sees it until you tell the network.
For how the transport itself works (server relay, delivery methods, distance based update rates), see Network Syncing.
The four ideas
| Idea | What it is | Use it for |
|---|---|---|
| Synced variables | Values the owner writes and everyone else reads, sent continuously and interpolated. | Positions, a lever angle, a score, anything that has a current state. |
| Network events | One off messages with a byte payload. | "A shot was fired", "the round started", "play this sound". |
| Ownership | Exactly one player owns each networked object and is the one allowed to write it. | Deciding who drives a pickup, who keeps the score. |
| Late joiners | Players who arrive after things changed. | Making sure they see the current state, not the scene defaults. |
If a player who joins five minutes later needs to see it, it is state and belongs in a synced variable (or must be re-sent to them). If it only matters at the moment it happens, it is an event.
The building blocks
BasisNetworkBehaviouris the base class for networked scripts compiled into the client. It gets a network ID, an owner, and the callbacksOnNetworkReady,OnNetworkMessage,OnOwnershipTransfer,OnPlayerJoinedandOnPlayerLeft.BasisNetworkShimis the same thing for Cilbox scripts. A Cilbox script cannot derive fromBasisNetworkBehaviour; it gets aBasisNetworkShimwithGetComponentand subscribes to its events instead.BasisSyncedObjectbuilds onBasisNetworkBehaviourwith a compact, interpolated state stream.BasisSyncedParameters,BasisSyncedTransformandBasisSyncedRigidbodyare ready made components on top of it.BasisPickupSyncNetworkingandBasisSeatSyncsync pickups and seats with no code.
A networked script, start to finish
using System;
using Basis;
using Basis.Network.Core;
using Basis.Scripts.Networking.NetworkedAvatar;
using TMPro;
using UnityEngine;
public class NetworkedCounter : BasisNetworkBehaviour
{
public TextMeshPro label;
int count;
public void Increment()
{
TakeOwnership();
count++;
Show();
SendCustomNetworkEvent(BitConverter.GetBytes(count), DeliveryMethod.ReliableOrdered);
}
public override void OnNetworkMessage(ushort playerId, byte[] buffer, DeliveryMethod method)
{
if (buffer == null || buffer.Length < 4) return;
count = BitConverter.ToInt32(buffer, 0);
Show();
}
public override void OnPlayerJoined(BasisNetworkPlayer player)
{
if (IsLocalOwner())
{
SendCustomNetworkEvent(BitConverter.GetBytes(count), DeliveryMethod.ReliableOrdered, new[] { player.playerId });
}
}
void Show() => label.text = count.ToString();
}Native scripts like this one work in client builds. In uploaded content, write the same thing as a Cilbox script (below).
What it shows:
- The player who presses the button takes ownership and becomes the one source of truth.
- The new value is sent as an event to everyone else (
Recipients = nullmeans everyone but you). - When someone joins, only the owner sends them the current value.
The same script for Cilbox is in Network Events.
Pages in this guide
- Network Events: sending messages, recipients, delivery methods, payloads.
- Synced Variables:
BasisSyncedParametersand customBasisSyncedObjectfields. - Ownership: who owns what, taking and checking ownership, what happens when the owner leaves.
- Late Joiners: getting new players up to date.
- Network Components: the no code sync components.
- Performance and Debugging: bandwidth, send rates and finding sync bugs.
Last updated on