Skip to content

Repository files navigation

rpc_await

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.

Documentation

  • This readme for a quick overview
  • The example scene for a working example
  • The code documentation for details

Installation

  • Add the addons/rpc-await folder to your project.
  • Add rpc_await.gd as an autoload or instantiate an RpcAwaiter node in your tree wherever you like.

Usage calling functions

  • Use send_rpc or send_rpc_timeout to 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!"

Usage for messages

  • Use send_msg or send_msg_timeout to send arbitrary data to a peer and get a response. This message can also be a Dictionary with msg data or just an int 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.add_message_listener(_message_received)

func _message_received(req: RpcAwait.RequestData):
	var my_data = req.data
	[...]
	req.result = my_result

If 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.

Notes

  • 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 and send_msg_timeout variants.
  • You can provide a default_return value to the rpc- and message-sending functions to specify the value that should be returned in case of a timeout or error.

About

Godot 4 script for an RPC layer that allows awaiting responses to requests.

Resources

Stars

25 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages