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-await
folder to your project. - Add
rpc_await.gd
as an autoload or instantiate it and place in your tree wherever you like.
- Use
send_rpc
orsend_rpc_timeout
to 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
@rpc
annotation:
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_msg
orsend_msg_timeout
to send arbitrary data to a peer and get a response. This message can also be aDictionary
with msg data or just anint
to 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_received
signal 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_received
may not useawait
themselves. 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_timeout
andsend_msg_timeout
variants.