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?
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:
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
[System.Serializable]
public class PlayerData
{
public string nick;
public int bla;
static XmlSerializer serializer = new XmlSerializer(typeof(PlayerData));
public string Serialize()
{
StringBuilder builder = new StringBuilder();
serializer.Serialize(
System.Xml.XmlWriter.Create(builder),
this);
return builder.ToString();
}
public static PlayerData Deserialize(string serializedData)
{
return serializer.Deserialize(new StringReader(serializedData)) as PlayerData;
}
}
And then RPC() the serialized result, and deserialize on the other end.
A simple usage test :
string xml;
void Awake()
{
PlayerData data = new PlayerData();
data.nick = "abc";
data.bla = 123;
xml = data.Serialize();
Debug.Log(xml);
}
void Start()
{
PlayerData data = PlayerData.Deserialize(xml);
Deebug.Log("{0} - {1}", data.nick, data.bla);
}
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
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
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.