Issue passing string[] array as RPC parameter
The whole answer can be found below.
Try Our
Documentation
Please check if you can find an answer in our extensive documentation on PUN.
Join Us
on Discord
Meet and talk to our staff and the entire Photon-Community via Discord.
Read More on
Stack Overflow
Find more information on Stack Overflow (for Circle members only).
Issue passing string[] array as RPC parameter
Merrik
2013-04-15 14:23:04
I noticed something strange with how string[] parameters are handled by RPC's.
When I send an RPC with an empty string[] it tries to find and RPC that takes no arguments. When I send an RPC with a string[] with one element it tries to find and RPC that takes a string.
Sending a string[] with more than 1 element is fine.
Here is my test case:
[code2=csharp]void Start() {
string[] s0 = { };
string[] s1 = { "A" };
string[] s2 = { "A", "B" };
photonView.RPC("StringRPCTest", PhotonNetwork.player, s0);
photonView.RPC("StringRPCTest", PhotonNetwork.player, s1);
photonView.RPC("StringRPCTest", PhotonNetwork.player, s2);
}
[RPC]
void StringRPCTest(string [] stringArray)
{
Debug.Log("Success: Received StringRPCTest " + stringArray.Length);
}[/code2]
The out put of which is:
PhotonView with ID 4 has no method "StringRPCTest" that takes 0 argument(s): PhotonView with ID 4 has no method "StringRPCTest" that takes 1 argument(s): String Success: Received StringRPCTest 2
Comments
I found a temporary fix in adding an extra parameter
[code2=csharp]void Start() { string[] s0 = { }; photonView.RPC("StringRPCTest", PhotonNetwork.player, s0, 0);
}
[RPC]
void StringRPCTest(string [] stringArray, int pointlessInt)
{
Debug.Log(" received StringRPCTest " + stringArray.Length);
}[/code2]
The problem are unfortunately the optional parameters of the RPC methods: [code2=csharp]public void RPC(string methodName, PhotonTargets target, params object[] parameters)[/code2]
Usually, they "help" by accepting parameters fitting the ones defined in your RPC. In this case, your string[] simply fits the "list of objects" definition (by accident, so to say). If you passed in a string-array with 4 items, your RPC implementation would have to use 4 string parameters.
To explicitly pass the string[] as single object, use this cast: [code2=csharp]photonView.RPC("StringRPCTest", PhotonNetwork.player, (object)s0);[/code2]
Ah, I see! thanks. That will save me many headaches in the future!
I am a little bit unclear about this. Can I have an array of floats and send those using RPC?
so...
photonView.RPC("someRPC",targets,(float[])myarray);
...
[PunRPC]
void someRPC(float[] myarray) {
...
}
is this correct?
According to what Tobias wrote, you can pass float[] with
photonView.RPC("someRPC",targets,(object)myarray);
This makes compiler use proper overloaded method.