Issue passing string[] array as RPC parameter

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

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).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

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

Merrik
2013-04-15 14:31:54

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]

Tobias
2013-04-15 15:24:01

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]

Merrik
2013-04-15 17:43:36

Ah, I see! thanks. That will save me many headaches in the future!

HooDoo
2016-04-30 00:56:53

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?

vadim
2016-05-06 15:22:24

According to what Tobias wrote, you can pass float[] with
photonView.RPC("someRPC",targets,(object)myarray);
This makes compiler use proper overloaded method.

Back to top