Any ideas how to use params (C#) in unity?

I’m trying to use the params feature of c# and I can’t get this to work at all :frowning:

public void TestRPC(RPCMode _RPCMode, object obj1, object obj2)
    {
        object[] objList = new object[3];
        objList[0] = "msg1";
        objList[1] = "msg2";
        objList[2] = "msg3";

        networkView.RPC("RPCTestEvent", _RPCMode, objList);
        
    }

    [RPC]
    public void RPCTestEvent(params object[] objects)
    {

        print("Length" + objects.Length);

        foreach (object anObject in objects)
        {

            print(anObject.ToString());

        }
        
    }

“params object[ ] objects” is something I’ve used in other C# projects and it’s always worked just fine, why doesn’t it work in unity?

The msdn reference is here:
http://msdn.microsoft.com/en-us/library/w5zay9db(VS.71).aspx

And the Unity reference says: “function RPC (name : string, mode : RPCMode, params args : object[ ]) : void” but again, params doesn’t seem to want to work, it’s as though unity unpacks the array and sends the contents individually, then forces the runtime to not allow you to use params to group them again.

Is this a Mono thing maybe? Or perhapse there’s a different way of using this in unity?

Help!

mono 1.2.5 or .net 2.0 thing yeah.

but additionally its because you can not just send through RPC what you would like to the list is very restricted and the data you want to send for example is not allowed!
See the RPC documentation to see which types of data can be sent

What I’m sending is nothing more than an object group of strings, they send just fine. The problem is that they’re unpacked on the recieveing end.

function RPC (name : string, mode : RPCMode, params args : object[ ]) : void

It even says there that u can use params and an object array.

Does that mean it will be supported in Unity3?

the params is how NetworkView.RPC is internally declared.

You use it by explicitely listing the parameters (and just the ones you want), the script reference on NetworkView.RPC shows you this. Thats how it works in U2 and U3, you can’t send arrays of any type. The supported types (and thats the complete list, not base type that can be serialized!) can be found in the manual - networked multiplayer - RPC Details

Sending as an array isn’t the problem, it’s recieving using the params function. I can send it as 30 different strings but I still can’t use the params function on the recieveing end.

Woot!

public void newRPCEvent(params object[] p)
    {

        System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(p.GetType());
        System.IO.StringWriter writer = new System.IO.StringWriter();
        s.Serialize(writer, p);

        networkView.RPC("RPCTestEvent", _RPCMode, writer.ToString());

    }

    [RPC]
    public void RPCTestEvent(string sString)
    {
        object[] objects = null;

        System.Xml.Serialization.XmlSerializer d = new System.Xml.Serialization.XmlSerializer(typeof(object[]));

        System.IO.StringReader reader = new System.IO.StringReader(sString);

        objects = (object[])d.Deserialize(reader);
        
        print("Length" + objects.Length);

        foreach (object anObject in objects)
        {

            print(anObject.ToString());

        }
        
    }

:twisted: