How to use MultiplayerSynchronizer in Godot

In this guide we explain how to use the MultiplayerSynchronizer node in Godot. This node makes updating and synchronizing variables for your multiplayer game incredibly easy. 

Making sure important variables are in sync across all players is an essential part of multiplayer game development and Godot makes this simple.

This guide was written using Godot version 4.3.


Prerequisites

For this guide you must be using at least Godot version 4.0.

Godot 3 is NOT supported for this guide.

You need to be comfortable with:

  • Navigating the Godot Editor
  • Scripting objects in GDScript
  • Multiplayer fundamentals of Godot
  • Creating and adding nodes to scenes

MultiplayerSynchronizer Node

Showing the MultiplayerSynchronizer node in the node creation window.

The MultiplayerSynchronizer is a node introduced in Godot version 4.0 which simplifies the development of multiplayer games.

It is used to synchronize properties (variables) from the multiplayer authority to all other connected players.

In this guide we are making our Enemy object synchronize its “health” float variable from the server to all connected players. When “health” is changed in code by the authority, it will replicate and appear for all other player’s clients automatically.

The enemy class that we are replicating the variable for. This shows the script in GDScript format.

The MultiplayerSynchronizer node is added like usual to any Scene in Godot by right clicking the node and clicking “Add Child Node”.

You should now see your new MultiplayerSynchronizer node as a child of your root node.

Creation of the MultiplayerSynchronizer node

With the MultiplayerSynchronizer selected, your editor should update and show a new bar at the bottom called Replication.

The editor window once the MultiplayerSynchronizer node has been selected

How to Sync Variables

To tell Godot which variables you want to be synchronized and updated to all of your connected players, we need to look at this bottom section shown in the image below.

The MultiplayerSynchronizer property Replication tab

As the middle section explains, when we want the MultiplayerSynchronizer to sync our variables across the network, we must add one to its list. The MultiplayerSynchronizer node does not replicate any properties on your object unless it is told to do. This is to improve the network optimization for your project.

There are a few ways we can add your variables to the sync list.

The easiest way is to click the “Add property to sync” button as highlighted here.

Adding properties to the MultiplayerSynchronizer property list using the Replication tab.

This now opens another menu called “Pick a node to synchronize”. We now select the node which contains the script where are variable is and press “OK”. In this example we are synchronizing the variables on the Enemy script, so we click Enemy and then “OK”.

Choosing a node to see the synchronizable properties

As you can see in the image, our enemy.gd script is listing the variables it contains. This is showing our “health” variable. We can also see all of the variables available from the node we inherit from. In this case our Enemy inherits from Node3D giving us access to position, rotation, scale, and more.

Please Note!

Object references, object IDs and resource IDs cannot be replicated! Attempting to replicate references to objects (such as objects extending Resource) will synchronize a broken “Encoded ID” value which results in undefined behaviour in your scripts!

As, in this guide we are replicating the “health“ variable, we click the “health” option and then click “Open”.

The search popup window showing the available properties to be replicated across the network

Property Spawn and Replication Preferences

Now our bottom Replication tab looks like this.

The Replication tab of the MultiplayerSynchronizer showing the newly added "health" property

We have the Node name, property name, sync on spawn checkbox, replication preference, and the button to delete the property from synchronization.

The first option we have to adjust is the sync on spawn toggle. When this is true the property is synchronized when it is spawned. If this is false the property is left to the default value until it is changed by the authority through script.

For our Enemy “health” value, we are leaving this checked so our enemy’s health is replicated to all of our players from the authority on the objects spawn.

The Replication tab of the MultiplayerSynchronizer node. Highlighting the Spawn preference tick box.

The second option is replication preference (A drop down box under “Replicate” in the Replication panel). This controls when the property will be replicated from the authority to the players. 

The default choice is “Always” which means this property replicates every time the MultiplayerSynchronizer updates even if the value is the same as the last update. 

The next choice is “On Change” which means that the MultiplayerSynchronizer node will only replicate this if the value is different from the last update. This is the most optimised choice for most properties as many only change infrequently through game scripts.

The final choice is “Never” which means this value is never synchronized from the authority to the players.

As our Enemy “health” value changes infrequently (only when our enemy takes damage) we are setting this to “On Change”.

After setting up our Enemy:health properties, our Replication tab now looks like this.

Showing the new enemy property being replicated On Change and with spawn ticked

MultiplayerSynchronizer Settings

The last step is to configure our MultiplayerSynchronizer node’s inspector settings on the right side of the Godot editor.

MultiplayerSynchronizer node inspector settings.

The two most important settings are “Replication Interval” and “Delta Interval”.

MultiplayerSynchronizer node inspector settings. Highliting Replication Interval and Delta Interval

“Replication Interval” is the frequency in seconds that this MultiplayerSynchronizer node replicates the “Always” properties from the authority to the other players.

When this is set to 0.0, the “Always” properties of this node replicate every network update (as fast as possible).

For example, 0.5 would update two times per second. 0.2 would be 5 times per second, and so on.

Delta Interval is the frequency in seconds that this MultiplayerSynchronizer node replicates the “On Change” properties from the authority to the other players.

Same as before, when this is set to 0.0, the “On Change” properties of this node replicate every network update (as fast as possible). Like before 0.5 would update two times per second. 0.2 would be 5 times per second etc.


Demonstration

In this demonstration, the authority sets the enemies’ health through a UI button. This “health” value is replicated and synchronized through the MultiplayerSynchronizer node and is displayed on all clients. You can see once the button is pressed, the UI text showing the health value updates on every player client.

A video demonstrating the health value we synchronized in the guide replicating and updating on screen for both connected players.

Conclusion

Now we have a basic property being automatically replicated and synchronized from the authority to all connected players.

Synchronizing variables is an essential part of multiplayer game development and with this knowledge you can begin to implement the fundamental network code needed to make your Godot project!

Many different types of variables can be replicated using the MultiplayerSynchronizer node making programming complex multiplayer games much easier.

Reminder

Object references, object IDs and resource IDs cannot be replicated! Attempting to replicate references to objects (such as objects extending Resource) will synchronize a broken “Encoded ID” value which results in undefined behaviour in your scripts!

Further Reading

We recommend having these resources at hand when working with the MultiplayerSynchronizer and other Godot multiplayer nodes:

The Godot 4.0 official introduction to multiplayer article

High Level Multiplayer in the engine Godot 4.x Stable documentation

MultiplayerSynchronizer Node Godot 4.3 documentation

Godot 4.3 documentation for the replication mode of a property

Be the first to comment

Leave a Reply

Your email address will not be published.


*