Search Unity

What type of parameters does NetworkView.RPC accept?

Discussion in 'Multiplayer' started by benblo, Oct 31, 2007.

  1. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    I'm trying to send data in a class via an RPC, but I keep getting this error :
    I made my PlayerData class Serializable, but still no dice.

    So I'm wondering, what are the allowed types?
    The method interface says params object[] args, so any object should do, but that would be too easy I suppose ;) !

    Should I manually serialize/de-serialize the data?
     
  2. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    280
    At the moment RPC can handle parameters of type integer, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion.

    If you are sending your own custom objects you could, for example, send each variable on its own and then construct the object in the RPC call.
     
    fprost likes this.
  3. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    OK... I had noticed you could send a NetworkPlayer (I use a RPC to associate PlayerData to a NetworkPlayer), that's why I hoped any class would do, like in a webservice.

    For anyone interested, you can use an XML serializer, the following works perfectly:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. using System.IO;
    5. using System.Text;
    6. using System.Xml.Serialization;
    7.  
    8. [System.Serializable]
    9. public class PlayerData
    10. {
    11.     public string nick;
    12.     public int bla;
    13.  
    14.  
    15.     static XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
    16.     public string Serialize()
    17.     {
    18.         StringBuilder builder = new StringBuilder();
    19.  
    20.         serializer.Serialize(
    21.             System.Xml.XmlWriter.Create(builder),
    22.             this);
    23.  
    24.         return builder.ToString();
    25.     }
    26.     public static PlayerData Deserialize(string serializedData)
    27.     {
    28.         return serializer.Deserialize(new StringReader(serializedData)) as PlayerData;
    29.     }
    30. }
    And then RPC() the serialized result, and deserialize on the other end.
    A simple usage test :
    Code (csharp):
    1.     string xml;
    2.     void Awake()
    3.     {
    4.         PlayerData data = new PlayerData();
    5.         data.nick = "abc";
    6.         data.bla = 123;
    7.  
    8.         xml = data.Serialize();
    9.         Debug.Log(xml);
    10.     }
    11.     void Start()
    12.     {
    13.         PlayerData data = PlayerData.Deserialize(xml);
    14.         Deebug.Log("{0} - {1}", data.nick, data.bla);
    15.     }
    16.  
     
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    It would be lovely to have that information available in the documentation of NetworkView.RPC()! I have just spent a little while twisting my mind to do a certain thing thinking I can't put NetworkViewIDs across RPCs when I remembered this thread ;-)

    Jashan
     
  5. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    if someone can explain me how exactly can network.rpc work, i woud apriciate this alot
    i got problems with it for ages, i cant get 1 light source to work with it
     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    networkView.RPC ("Command name to execute on the receiver", RPCMode, params);
    A player sends an RPC to a destination.
    RPCmode = the destination (receiver) ... can be : Server, Others, All.
    Depending to who you are sending the RPC, the receiver will execute the command.