- Home /
Passing variables back and forth in client server environment using RPC
I have seen quite a bit of examples and documentation until now regarding rpc and client server however, there's a very stupid and very obscure subject on which I hope those good amongst you can shed some light: requesting a server variable from a client, and updating said value... when the scripts are in different gameobjects.
I have seen quite some examples of RPC calls when the script calls an rpc function on that same script on the server or .others, but what about calling an rpc function somewhere else, asking for a variable value, or trying to set up a variable there?
My attempts at making a STATIC rpc function, or even a very stupid "return server variable" rpc function have failed miserably, especially since RPC returns always VOID.
Please help, thanks.
This is basically the code that I need to use, but that doesn't update my needed variable, when run client-side.
private int variable = 0;
void OnNetworkLoadedLevel () {
if (Network.isServer) {
print("As a server");
variable = returnvariable();
}
else {
print("As a client");
networkView.RPC("returnvariableRPC",RPCMode.Server, Network.player);
}
print (variable + " doesn't hold the value it should");
}
private int returnvariable() {
do_stuff();
}
[RPC]
public void returnvariableRPC(NetworkPlayer player) {
int localvariable = do_stuff();
networkView.RPC("assignvariableRPC",player,localvariable);
}
[RPC]
public void assignvariableRPC(int parameter) {
variable = parameter;
}
Perhaps you would consider showing some of your code, so we might have a look at it and see if we can spot the problem?
I've basically added all the code I've come up with. Basically the client never receives the call "back" from the server, apparently. I have not found a way to make it so that the client receives the server's actual variable value.
Answer by roamcel · Aug 03, 2011 at 03:41 PM
Ok I've understood what's going on. The code is correct, but there's a timing problem. In particular, here
else {
print("As a client");
networkView.RPC("returnvariableRPC",RPCMode.Server, Network.player);
}
print (variable + " doesn't hold the value it should");
networkView.RPC will run and will immediately return focus to the OnNetworkLoadedLevel, without waiting for completion. variable WILL actually be correctly updated... but long after the calling routine has completed. How do I fix this? Is there a way to put the script to 'sleep' until networkView.RPC completes the call, or I'm just using the wrong approach? It feels like the second option, actually. As a matter of fact, I'd rather have the connection time out rather than the script going forward without the expected variable value.
Quite clearly the only "solution" is to implement the follow-up code into the "returnvariableRPC" function, which will run on the client that instantiated that very call. However it doesn't feel like the right way to go, actually.
Your answer
![](https://koobas.hobune.stream/wayback/20220612164702im_/https://answers.unity.com/themes/thub/images/avi.jpg)