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.
- 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 it and place 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 = RpcAwait.send_rpc(target_net_id, _do_some_work)- The peer needs to have this function, but no need for the
@rpcannotation:
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.message_received.connect(_message_received)
func _message_received(req: RpcAwait.RequestData):
var my_data = req.data
[...]
req.result = my_result- The signal handlers of
message_receivedmay not useawaitthemselves. 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.