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.

circle-info

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.

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

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

chevron-rightDetecting Changeshashtag

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

circle-info

Note: This will only be triggered on passive object

triangle-exclamation
chevron-rightSynchronizing local variableshashtag

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.

triangle-exclamation


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.

circle-exclamation

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...

circle-info

By defalue all NetworkVaraibles will be using Reliable DelivaryMode

chevron-rightChange Delievry Mode from Prefab Databasehashtag

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

chevron-rightChange Delievry Mode in codehashtag

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

triangle-exclamation

chevron-rightChanges Detection Mode (In Reliable Variables)hashtag

🛠️ 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:

circle-info

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 Warpped Variables you can force an individual variable to be included in the next synchronization cycle by calling:

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


FAQ:

chevron-rightWhat if I want to modify a variable from a passive object?hashtag

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 RPC to the active object and let it handle the modification. This ensures that the update is properly synced for everyone.

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

Last updated