How do Godot 4's multiplayer nodes work under the hood?

I’m curious about how the new multiplayer nodes work under the hood, but I’m inexperienced with C++ and am not confident that I understand the source code.

Do the multiplayer nodes differ from using RPCs to spawn nodes and sync properties? If so, how? If not, what kind of RPC setup are these nodes equivalent to?

I took a dig at MultiplayerSynchronizer for fun to see how it works.

The first thing I’ve noticed is that MultiplayerSynchronizer itself actually doesn’t do any synchronization, it just provides data, like what’s changed in get_delta_state.

This is then used by SceneReplicationInterface::_send_delta, which encodes and compresses the properties that have changed. The actual data sending goes through a bunch of steps:

  1. SceneReplactionInterface::_send_raw
  2. SceneMultiplayer::send_command
  3. SceneMultiplayer::_send

The above then ends up calling the MultiplayerPeer’s put_packet method directly, which depends on which implementation you’ve chosen. Here’s ENetMultiplayerPeer::put_packet for example.

So to conclude, it just creates and sends the packets directly.

1 Like

I will add that for spawning nodes you would use a MultiplayerSpawner.

Under the hood it listens for added child signals and if the node matches a what is on the configuration list it will send a reliable rpc call to spawn on the remote scenes.

There is some more fancy stuff going on, because if there are lots of developments within the game and a new client joins the MultiplayerSpawner’s will get to work on syncing state for the new client.

I don’t seen any benefits to using rpc over the multipleplayer nodes. But can be useful in other scenarios.

Because if you are writing scripts to mimic native code it will be inherently slower.

1 Like

Thanks for the info! This is exactly the kind of stuff I wanted to know. Sorry for not making more of an effort to understand the code myself.

Thanks for taking the time to look at that. I wanted to select both of your comments as solutions, but unfortunately I could only pick one.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.