- Home /
Networking RPC calls never sent to other clients
Hello,
I've been leaning up against the Unity scripting reference for networking lately, and I've set up a server and clients. The clients report they've successfully connected to the server every time, but none of the RPC calls make it out of the calling client and over to the other clients.
Here's my server initialization script: http://pastebin.com/q0hNRkXq
And here's my client's "connect to server" script, which sends two RPCs: http://pastebin.com/H2Wg6ZWc
Lastly there's the client-side script that handles incoming RPCs: http://pastebin.com/vzwMkSzn
My problem is: when sending RPCs with any other RPCMode's than RPCMode.All and RPCMode.AllBuffered, I see no result. That indicates that the RPC never reaches any of the other connected clients and the only receiver of the RPC is itself.
As I've been leaning up against the Unity scripting reference (http://docs.unity3d.com/Documentation/Components/NetworkReferenceGuide.html), why it doesn't work is greatly confusing to me - because I've done as the reference says, as far as I'm aware of.
Do I need some sort of RPC forwarding script on the server? Why can't two connected clients communicate when they're connected to the same server?
I'll greatly appreciate any attempted help!
Thank you very much
Answer by Bunny83 · Nov 25, 2012 at 11:21 PM
Of course that doesn't work because you only create your player object locally. RPCs are sent from an object's networkView to it's counterpart on another client. The NetworkViewID you allocate is valid on all clients, but it's not assigned to an object on the other clients. Therefore your RPCs you send are lost because there's no receiver.
The usual way is to either have a "communication object" in a scene and every peer loads this scene. NetworkViews on scene objects automatically get a NetworkViewID which is the same on each client.
Another way is to use Network.Instantiate which takes care of instantiating th object locally as well as on all other clients. In addition it will sync the NetWorkViewID(s) which automatically gets allocated for the object.
I don't really like Network.Instantiate since it uses a buffered RPC which can't be removed that easy. I always use a communication object in a scene.
edit
I've made (another) very simple chat example script. Just create an empty GO in a scene and attach the script. It requires a NetworkView which should be automatically attached. Build your game and make sure the scene that contains the GameObject with the script, is the first one in the scenes list (in the build settings).
Start 2, 3 or most instantex of your game and make one the server. Start the server and connect the other clients to the server.
Hi, thank you. I used Network.Instantiate(); first, but I had issues with it and it never worked properly AT ALL. I couldn't find anyone with simular issues through googling... So now I'm using RPCs. Do you think you can give more information on your "communication object" (perhaps upload some example code, or explain how to do it properly)? Thank you very much for your time and help!
I don't have time, but the important thing is that every peer (clients and server) load the same scene. This scene should contain an emptyGO with a networkview. This networkview will get linked to all other connected clients automatically when they load this scene.
If i can find the time i will post a sample
Thank you, I was helped by Zerot to make this work and it's now fully functional. I couldn't be happier!
Just like to add that RPCs are automatically forwarded by the server to the right target. The main problem with RPCs is that the Unity API only provides context sensitive RPCs. So an RPc can only be send from a NetworkView from one client to a NetworkView on another client which has the same NetworkViewID.
Sending RPCs to all objects or something like that doesn't exist in the API. Unity however has something else internally which enables Network.Instantiate to work without a context object.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
RPC is called but it doesn't destroy some GameObject instances 1 Answer
client to client ping - NAT punchthrough 1 Answer
Can you tell OnSerializeNetworkView who the owner is? 3 Answers
Spawning Clients 1 Answer