At a high level, a
NetworkVariable is a variable with its value tracked by the Netcode SDK. Its values are replicated to other clients connected to the server regularly. When a client connects initially to a host, all relevant
NetworkVariable latest values "state" will be replicated to that new client. Your state gets updated at regular intervals.
NetworkVariable can be referenced in this doc as "state" or as "NetVar" or as "replicated variable".
If you change your variable's value on your side, others will eventually see the latest value on their side.
The data can be custom containers and complex structures such as inventory structs.
By default, Netcode comes with two different containers.
NetworkVariable container is built to store simple
unmanaged data types such as
ints. The List implementation is a wrapper around the .NET equivalent. It is event-driven and has a list of events to be synced. The default implementations come with flexibility in terms of settings. Containers can be setup to sync client-to-server, server-to-client, or bi-directional. It can also be set to target specific clients using custom delegates.
NetworkVariable container is a wrapper container around the stored value, the value has be accessed using the
NetworkList and implementations are primarily designed as samples showing how to create
NetworkVariableBase structures. The
NetworkVariable<T> container is however considered production ready for simple
You must remember to add the
NetworkObject component to the
GameObject to which your script belongs.
To create your own
NetworkVariable container, simply create a class deriving from
NetworkVariableBase and declare it as a field of a
NetworkBehaviour. To learn how to write your own containers for more complex structures, see the
NetworkVariable<T> implementation. To learn how to do custom delta encoding on complex structures. See the
When the value of a
OnValueChanged callback gets invoked with previous and the new value. This would give us a chance to react to value changes without continuously polling its latest value.
By default, a
NetworkVariable<T> can be written by the server only and read by everyone. However, these permissions can be changed via the constructor to allow other use cases.
Everyone→ All clients and server will get value updates.
Owner→ Only server and the owner client will get value updates.
Server→ Only the server can write to the value.
Owner→ Only the owner client can write to the value, server can't write to the value.
I want owner to move the cube but server to color it!
So, we're going to have 2 netvars: one server-writable and another owner-writable one.