- Home /
send complex objects over RPC
Hello,
are there any good solutions to send complex objects (e.g. custom playerInfo-Class) through a RPC. The manual states that RPC-Parameters can only be of simple types like string, float, etc.
a) My first idea would be to serialize the complex object to XML or JSON, send it as String through the RPC and deserialize it on the other side.
b) Other Idea: Is it possible to use an existing unity-networkview and access it's raw socket-connection and send data over this connection?
Are there better/more performant ways to do this?
The RPC's are not called frequently, just at level-start/level-end (or something similar).
regards
Answer by jashan · Mar 04, 2010 at 10:05 PM
How "complex" do you want these classes to be - and how complex do they really need to be?
What I'm doing for Traces of Illumination is simply have a couple of RPCs with a lot of parameters each parameter representing one member variable of the objects I want to transmit. As in your case, these objects are usually just transmitted once during login (there, you get a lot of objects) and then when another player logs in (one PlayerInfo to all existing players). So it's not really a problem.
And usually, you shouldn't have too many member variables that really need to be transmitted. I haven't counted but I think those are around 5 to 15 parameters (and it's all simple types because my member variables are only simple types).
If you need "structures" (like, I have GameGroups, Teams and Players, each game group has multiple teams, each team multiple players), you can have custom IDs that you transmit via multiple calls ("RPCAddPlayerToTeam(int playerID, int teamID)") - then, of course, you'll usually want to use some sorts of lookup tables to get everything together (but usually, you'll need such lookup tables anyways). I'm using the same RPCs here for initial transmission of the structures to new players that I'm also using when actually creating the structures in first place (one could probably use buffered-RPCs but I just don't trust those ;-) ).
Answer by Ashkan_gc · Mar 04, 2010 at 08:48 PM
you can create your own connections and use System.Net.Sockets in whatever way you want but you can not access the socket of networkViews. the first solution is the best one. serialize the class and send it and deserialize it again. you can just create a string of variables that you need and not all of them to save bandwidth.
Answer by Murcho · Mar 05, 2010 at 03:20 AM
In my previous project we tackled this problem by serializing into XML strings like you suggested, however there is a limit on how long the string could be, so simply by testing for this limit before sending, we fixed this by sending an int in the first packet as well which told the recipient how many strings to expect before joining and de-serializing the data.
We only used this for loading information, basically we had a modular weapon system and it sent the data of how to put together the client's weapon. For anything you need to send regularly I would recommend simplifying your data as much as possible as this requires a lot of work on the CPU side with string manipulation.
Answer by Lance Sun · Mar 05, 2010 at 06:20 AM
To serialize objects to xml, try xstream-dot-net (http://code.google.com/p/xstream-dot-net/)
Your answer
![](https://koobas.hobune.stream/wayback/20220612094602im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Acess server stuff by the client 0 Answers
How to use Unet to send network messages like RPC (Javascript) 0 Answers
When to use OnSerializeNetworkView and RPC's 1 Answer
Help with Network Script. Game with multiple Maps. 2 Answers
Send a mesh via Network 2 Answers