Network Variables

General:

ObjectNet provides the facility to keep variables synchronized over the network.

Those variables are automatically synchronized over the network from Active to Passive instances.

To implement Network Variables, the script must be inherited from NetworkBehaviour

Network variables can synchronize the following types:

int   uint   long   ulong   short   ushort   float   double 
-------------------------------------------------------------
byte   sbyte   byte[]   bool   string   char   char[]   enum
-------------------------------------------------------------
Vector2   Vector3   Vector4   Quaternion   Color   Matrix4x4

can be used in two ways.

By NetworkPrefabs Database example

Network Variables by code:

To use Network Variable in code they must bound a type NetworkVariable<T> where T must be included into allowed types.

private NetworkVariable<int> variable = 0;

Once defined you can use it as if any other script variable

 this.variable += 1;

 int variableValue = this.variable;

This provides greater control and access to various callbacks you can leverage. Below are some examples:

Detecting Changes

You can detect when the object was updated on the passive instance to apply changes only when occur.

 this.variable.OnValueChange((int oldValue, int newValue) => {
  Debug.Log($"Value was updated from {oldValue} to {newValue} ");
 });

Note: This will only be triggered on passive object

Synchronizing local variables

ObjectNet allows to synchronization of network variables with local variables automatically. The following script allows you to keep a local variable synchronized with a network variable, no matter where you change the value, both variables will have the same value.

NetworkVariable<string> variable = "Hello world";
string localVariable = "Start text";

 this.variable.OnSynchonize(() => { return this.localVariable; },
                            (string value) => { this.localVariable = value; });

// In this case variable Network will also receive the new value "New value from local"
this.localVariable = "New value from local"; 
// In this case local variable will also receive the new value "New value from Network Variable"
this.variable = "New value from Network variable";


Variables DeliveryModes:

there are two Modes NetworkVariables uses Reliable and Unreliable Delivery Mode.

Reliable variables send data only when it detects changes or when a new client join, This makes them ideal for data that changes infrequently, as they reduce bandwidth usage and ensure consistency.

Examples of Reliable variables:

Health, Player name / username, Score / Points, Kill / death count (K/D), Game state (Win/Loss, Start Match, Pause), Quest or mission updates, Timers, lobby settings, Inventory items, map selection, Skins selection, Environmenatl states (e.g., doors open) , etc...

By defalue all NetworkVaraibles will be using Reliable DelivaryMode

Change Delievry Mode from Prefab Database

You can change the Network Variable Delivary Mode from the Network Manager Prefab DataBase as shown in the second Image

Change Delievry Mode in code

You can change the Network variable Delivary Mode from Reliable to Unreliable through code

private NetworkVariable<int> variable = 0;

private void Awake(){
    this.variable.SetDeliveryMode(DeliveryMode.Unreliable);
}

Changes Detection Mode (In Reliable Variables)

🛠️ Automatic Detection Mode

By default, all Reliable Variablesin ObjectNet use Automatic Detection Mode. In this mode, ObjectNet continuously monitors variable changes and efficiently packs all detected changes into a single buffer, reducing bandwidth usage and improving performance.

The detection rate is tied to the Object send rate, ensuring that:

  • Only variables with actual changes are included in the update buffer.

  • If no changes occur, no network packets are sent, optimizing traffic flow.


🛠️ Manual Detection Mode

When you mark a variable as Manual Detection, ObjectNet will not sync it automatically. The developer take full responsibility for sending these reliable variables by themself.

To send all Manual Reliable variables across all objects use:

NetworkManager.Instance().DetectVariablesChanges(bool includeAutomatic = false);

This method does not immediately send variable data. It simply Invalidate all variables, allowing them to be detected and included in the next regular send cycle.


📝 Note:

If you are using you can force an individual variable to be included in the next synchronization cycle by calling:

variableName.Invalidate();

This manually flags the variable as changed, ensuring it is sent even if its value hasn't been modified.


FAQ:

What if I want to modify a variable from a passive object?

You cannot directly modify a variable on a passive object, as the change will not be synced across other clients. To update the value correctly, you should send an to the active object and let it handle the modification. This ensures that the update is properly synced for everyone.

NetworkVariable<int> health = 100;
// Anywhere in your code (on active or passive)
NetworkExecute<int>(Damage, 20);
// This ensures the value is only changed by the active instance
private void Damage(int damage){
    if (IsActive())
        health -= damage;
}

💡 Even passive objects can call NetworkExecute, but the logic inside will only run on the active side — keeping everything in sync.

Last updated