- Home /
Generic RPC Call
Hi,
I'm trying to make an networkview.RPC call with a parameter that can be anything. I can't seem to get it to work though. I tried several things, but none worked:
My first try,
networkView.RPC("TestRPC",RPCMode.AllBuffered, newValue); ...
[RPC] void TestRPC(object newValue) { Debug.Log ("RPC: " + newValue); }
But this gave me an error:
Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'System.Object' but got 'UnityEngine.Vector3' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
Which makes sense, though still sucks the Vector3 isn't just cast to an object which should be possible. So I tried to cast it myself:
networkView.RPC("TestRPC", RPCMode.AllBuffered, (object)newValue); // TestRPC method staid the same
Which gave me exactly the same error. Which also makes sense, since object is still a Vector3 and gets serialized, apparently during the deserialization it'll make it a Vector3 again (I think, if I'm wrong here please do tell.)
So then I tried another thing, c# has some great feature, generic methods, which I then tried:
networkView.RPC("TestRPC", RPCMode.AllBuffered, newValue); ...
[RPC] void TestRPC(T newValue) { Debug.Log ("RPC: " + newValue); }
And this gave almost the exact same error, only now it wants a T...
Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'T' but got 'UnityEngine.Vector3'
Then I though, let's just make multiple "TestRPC" methods with different parameters:
networkView.RPC("TestRPC", RPCMode.AllBuffered, newValue); networkView.RPC("TestRPC", RPCMode.AllBuffered, 5); networkView.RPC("TestRPC", RPCMode.AllBuffered, 3.1415f);
[RPC] void TestRPC(Vector3 newValue) { Debug.Log ("RPC v3: " + newValue); }
[RPC] void TestRPC(int newValue) { Debug.Log ("RPC int: " + newValue); }
[RPC] void TestRPC(float newValue) { Debug.Log ("RPC fl: " + newValue); }
Then the first RPC call worked, it could find the Vector3! Though the other two failed:
Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'UnityEngine.Vector3' but got 'System.Int32' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
and
Sending RPC failed because 'TestRPC' parameter 0 didn't match the RPC declaration. Expected 'UnityEngine.Vector3' but got 'System.Single' UnityEngine.NetworkView:RPC(String, RPCMode, Object[])
Then I got another idea: create a little wrapper for object and use that:
public class RPCObject //Tried the same with struct { object _Obj;
public object Obj
{
get
{
return _Obj;
}
set
{
_Obj = value;
}
}
public RPCObject(object obj)
{
_Obj = obj;
}
}
... networkView.RPC("TestRPC", RPCMode.AllBuffered, new RPCObject(newValue)); ...
[RPC] void TestRPC(RPCObject newValue) { Debug.Log ("RPC v3: " + newValue.Obj); }
But this told me that I can't use RPCObject...
Sending RPC failed because 'TestRPC' parameter 0 (RPCObject) is not supported.
Then I found a solution! And this is it:
void CallNetworkViewRPC(string fieldName, T newValue) { System.Type curValType = curValue.GetType(); if (curValType == typeof(Vector3)) { networkView.RPC("TestRPCVector3", RPCMode.AllBuffered, newValue); } else if (curValType == typeof(int)) { networkView.RPC("TestRPCInt", RPCMode.AllBuffered, newValue); } else if (curValType == typeof(float)) { networkView.RPC("TestRPCFloat", RPCMode.AllBuffered, newValue); } }
[RPC] void TestRPCVector3(Vector3 newValue) { Debug.Log ("RPC v3: " + newValue); }
[RPC] void TestRPCInt(int newValue) { Debug.Log ("RPC int: " + newValue); }
[RPC] void TestRPCFloat(float newValue) { Debug.Log ("RPC fl: " + newValue); }
So basecly make a method for each needed variable and call the correct one depending on it's type. This does kind of suck, since now I have to make a whole lot of methods for such a silly little thing...
So, is there a better way of doing it than my working solution?
Note: While writing this I read that apparently the only possible variables send are int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion. This would make my working solution not that bad to make, but still the question stands, though now more out of curiosity.
For those whom are interested, all the code above are just some tests I was running, the actual thing I'm trying to make is a RPC call that can work for any property:
public Vector3 _WantedDirection; public Vector3 WantedDirection { get{ return _WantedDirection; } set { NetworkChangeProperty("_WantedDirection", ref _WantedDirection, value); } }
void NetworkChangeProperty(string fieldName, ref T curValue, T newValue) { if (!curValue.Equals(newValue)) { if (Network.peerType == NetworkPeerType.Disconnected) { curValue = newValue; } else { networkView.RPC("NetworkChangePropertyRPC", RPCMode.AllBuffered, fieldName, >newValue); } } } [RPC] void NetworkChangePropertyRPC(string fieldName, object newValue) { this.GetType().GetField(fieldName).SetValue(this, newValue); }
Ok, that's all the information I can give I think. If anything still isn't clear, just ask.
Thanks a lot to the people who read all the way to here.
-Pablo
Ok, I just posted this, and now all my code parts are broken... If it's not just fixed, I'm doing it right now
Answer by Kryptos · Jul 28, 2012 at 07:31 PM
As you can see networkView.RPC takes the name of the method in parameter. And that is the only thing. Therefore you cannot overload the method with different parameters type.
Yes I know that now, after trying a lot of stuff as you can read :P But you are saying there is no other way around than what I did?
Your answer
Follow this Question
Related Questions
Problem with Network Connection 1 Answer
RPC not being called 1 Answer
Clearing RPC buffer on a NetworkView 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers