Small layer for Godot 4 RPC to allow making rpc-calls or sending messages to peers that you can await on for a return value.
Because sometimes you just want to request something from a client and await the result to continue instead of spreading your code over multiple functions that call each other over different machines.
Requires Godot 4.4. If you are using Godot 4.x earlier than that you must use rpc_await 1.0.
- This readme for a quick overview
- The example scene for a working example
- The code documentation for details
- Add the
addons/rpc-awaitfolder to your project. - Add
rpc_await.gdas an autoload or instantiate anRpcAwaiternode in your tree wherever you like.
- Use
send_rpcorsend_rpc_timeoutto call a function on the same location in the scene tree of the peer:
var result = await RpcAwait.send_rpc(target_net_id, _do_some_work)- The peer needs to have this function and it needs the correct rpc annotation to be accessible ("any_peer" or "authority"):
@rpc("any_peer")
func _do_some_work() -> String:
await get_tree().create_timer(2).timeout # You can use await on this side, too.
return "My Answer!"- Use
send_msgorsend_msg_timeoutto send arbitrary data to a peer and get a response. This message can also be aDictionarywith msg data or just anintto specify a message type.
var result = await RpcAwait.send_msg(target_net_id, my_data)- Handle these requests on the peer by connecting to the
request_receivedsignal and fill in the result property with your result:
func _ready():
RpcAwait.add_message_listener(_message_received)
func _message_received(req: RpcAwait.RequestData):
var my_data = req.data
[...]
req.result = my_resultIf you frequently add and free nodes that are registered as message listeners you should make sure to use RpcAwait.remove_message_listener() in the _exit_tree() handler or when you free() your nodes.
RpcAwait.default_timeout_secs[default 5.0] can be changed to suit your needs. Values <= 0 disable the timeout.- Give a custom timeout value for specific calls using
send_rpc_timeoutandsend_msg_timeoutvariants. - You can provide a
default_returnvalue to the rpc- and message-sending functions to specify the value that should be returned in case of a timeout or error.