- Home /
how to ask Server for a value?
I need to ask a lock from client to server. Basically it would be a get function but RPC doesn't support that.
One solution would be to have a dedicated variable, but there is a wide number of locks I can get and I need to specify the return variable in the client. Also, the actual request for the lock comes from a range of classes that use the lock handler class.
A local version is:
public PhaseLock getLock(GamePhases phase){
PhaseLock newLock=generateLock(phase);
lockHolder[phase].Add(newLock.id);
return newLock;
}
Someone knows a way to do it in a client-server environment without writing a thousand lines of code to store and pass the identity of the script (and the gameobject instance) that requested the lock?
Answer by hubatish · Apr 29, 2014 at 12:49 PM
Why is it difficult to know which gameobject/script sent the RPC? Generally, my clients and server have near duplicate scenes - or at least they should each have an instance of whatever object you need to put a lock on. So knowing the correct script/gameobject to send to is as simple as passing making an RPC call from a script watched by a NetworkView on the client being received by the same script on the server.
To "get" the information from the server is really two calls - one to request the data from the server and one to send it back. So your code might look something like this:
private void RequestLock()
{
networkView.RPC("GetLock", RPCMode.Server, "LockType");
}
[RPC]
public void GetLock(string lockType, NetworkMessageInfo info)
{
//get the correct lock...
networkView.RPC("ReceiveLock", info.sender, aLock);
}
[RPC]
public void ReceiveLock(string aLock)
{
//do good stuff with the lock
}
The NetworkMessageInfo parameter provides which player and which networkView sent an RPC call. If it's difficult to reconstruct the lock from strings or some array of basic objects, you might want to look into OnSerializeNetworkView, which you can use to pass more complicated objects.