Hi there,
I’ve been having a pretty bad week-end because Unity’s been crashing on me about 20 or so times (a few of those crashes filed as bug reports). I think I figured out one particular problem I need a solution for:
Since I need fine-grained control on what I’m sending to whom (different game sessions taking place on one server, some player participating, others not), I’m trying to create a networking layer on top of the Unity networking API.
I would like to be as close as possible to the original networking approach, so I basically have a class JCsNetwork which has a method “RPC” which takes a NetworkView, RPC-Name and params object[ ] args. This method then delegates to RPCToClients which does the actually sending out.
On the server, this works smoothly (as far as I can see - RPCToClients is directly called as a method). However, when this method is called on the client, it needs to RPC the args to the server so the server can do the distribution (because I only have the NetworkPlayers on the server). So, on the client, RPCToClients is called through an RPC, passing the parameters that RPC has received.
Let me illustrate that with actual code:
public void RPC(NetworkView nv, string rpcName, RPCMode mode, params object[] args) {
if (Network.isClient) {
networkView.RPC("RPCToClients", RPCMode.Server, nv.viewID, rpcName, args);
} else {
NetworkMessageInfo nmInfo = new NetworkMessageInfo();
RPCToClients(nv.viewID, rpcName, args, nmInfo);
}
}
}
This is a bit simplified - in particular, I have removed handling of the RPCMode (it’s converted to int and then passed to RPCToClients).
Then, there’s:
[RPC()]
public void RPCToClients(NetworkViewID viewID, string rpcName, int rpcMode, object[] args, NetworkMessageInfo info) {
...
Now what seems to happen is that when I RPC to that, my args are somehow converted to a byte-array instead of the original parameter-list. When I then put this into the RPCs that go to the clients (in a loop over the network players that are in that particular game session), I get BOOM - a crash of the editor (I’ve had a few other problems, too - seems to me this is “weekend of Unity crashes” for me).
As far as I can tell from my logs, this crash is right at:
nv.RPC(rpcName, player, args);
Where nv is the NetworkView (which I’m getting through the NetworkViewID), rpcName obviously is the name of the RPC, player is the NetworkPlayer (I’m looping over), and - that seems to be the thing that’s making the editor crash: args is “something in System.Byte[ ]”. It should be the object-array of my original parameters. Maybe I should note that there were ints sent, some of which are negative.
The question now is: Is there any (convenient) way of creating a generic RPC that could take any number of the typical RPC parameters (bool, int, Vector3, NetworkView etc.), so I can pass this along into another RPC? I mean, something more elegant than encoding all parameters into some string (or byte[ ] which should work in 2.0.2) and than decoding it on the server?
I somehow need a way to model “RPCMode.All” and “RPCMode.Others” so that clients can send stuff through my networking layer. I’d like to just replace networkView.RPC(…) with JCsNetwork.RPC(networkView, …). Which would be very nice from the “software-designer’s point of view”.
I’ve also tried:
[RPC()]
public void RPCToClients(NetworkViewID viewID, string rpcName, int rpcMode, NetworkPlayer sender, params object[] args) {
...
But I’m still getting this nasty Byte[ ] in args (which is not what I’ve sent!)
Anyone with any ideas? Larus???
Still sunny regards
Jashan