Multiplayer in Godot 4.0: RPC syntax, channels, ordering

godotengine.org
5 min read
fairly easy
New RPC syntax and features in Godot 4.0. Introducing channels and ordered transfer mode.
The new `@rpc` syntax in Godot 4.0

Multiplayer in Godot 4.0: RPC syntax, channels, ordering By: Fabio Alessandrelli

Howdy Godotters! Time for another update on Godot 4.0's multiplayer networking.

We have been really busy working on the foundation of the networking and multiplayer classes lately, and there are quite a few new features to talk about. In this post, we'll start by showing some of the new RPC syntax and features.

Simplified RPC configuration

First of all, many users found the old master and puppet keywords in 3.x were confusing.

The master keyword would mean that a function could be called on the "network master", while puppet that a function could be called only on the "non-master" peers. Additionally, the old master keyword had very little usage, since remote could be used in its place with little to no effort.

Learning from this, we decided to have a unified @rpc annotation with few optional parameters.

Authority

@rpc func my_rpc(): print("RPC called.")

By default, @rpc only allows calls from the multiplayer authority, which is the server by default. You can optionally set the multiplayer authority on a per-node basis via the Node.set_multiplayer_authority() method.

In this sense, the @rpc annotation alone behaves like the old puppet keyword.

@rpc(any) func my_rpc(): print("RPC called by: ", multiplayer.get_remote_sender_id())

As mentioned above, the @rpc annotation can also take optional parameters. If one of those parameters is any , the RPC will be callable by any connected peer, behaving like the old remote keyword. You can get the ID of the peer that is making the call via the MultiplayerAPI.get_remote_sender_id() method.

@rpc(any) func my_rpc(): var peer_id = multiplayer.get_remote_sender_id() if peer_id == get_multiplayer_authority(): # The authority is not allowed to call this function. return print("RPC called by: ", peer_id)

There is no direct replacement for the rarely used master keyword. In those cases, @rpc(any) can be…
Godot Engine
Read full article