I'm developing a MMO game with a dedicated server. I'm using RPC calls to communicate between the client and server using rpc_id() calls. I have a need to make requests to the server and receive a response. As far as I can tell from the docs and experimenting, rpc_id() calls don't return values from remote methods So I'm using RPC calls from the server to callback methods in the client to pass back results.
Ideally, I'd like the client method (e.g. validate_user shown below) to suspend after the RPC call until the response from the server is received. According to the docs, yield(object, "signal") will wait for the signal then return the parameters sent by the signal. So I thought
val_result = yield(self, "signal_name")
would accomplish that. But it is not working. The yield() returns immediately with a method status. I've tried several different variations using continue() but cannot make it work. I do have a work around which is pretty ugly but would prefer a more concise code pattern similar to that below.
Any and all suggestions are welcome.
Client Side
signal validate_user_return(_uauth);
func validate_user(_username:String, _password:String)-> void:
# send credentials to server
rpc_id(1, "validate_game_user", _username, _password);
# Wait for server callback and return validation result -- DOES NOT WORK
var val_result:Dictionary = yield(self, "validate_user_return");
remote func validate_user_callback(uauth:Dictionary)->void:
self.emit_signal("validate_user_return", uauth);
Server Side
remote func validate_game_user(username:String, password:String)-> void:
var caller_id = get_tree().get_rpc_sender_id();
var uauth:Dictionary = d.validate_user(username, password);
# send the result back to the caller
rpc_id(caller_id, "validate_user_callback", uauth);