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).
asked Mar 04 '10 at 08:34 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 ;-) ).
answered Mar 04 '10 at 10:05 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.
answered Mar 04 '10 at 08:48 PM
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.
answered Mar 05 '10 at 03:20 AM
To serialize objects to xml, try xstream-dot-net (http://code.google.com/p/xstream-dot-net/)
answered Mar 05 '10 at 06:20 AM