Issue passing string[] array as RPC parameter
Options
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:
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
0
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]0 -
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]0 -
Ah, I see! thanks. That will save me many headaches in the future!0
-
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?0 -
According to what Tobias wrote, you can pass float[] with
photonView.RPC("someRPC",targets,(object)myarray);
This makes compiler use proper overloaded method.0