- Home /
RPC and multiplayer networking
I read the manual from the official documentations.
But it only roughly introduces what it is. I have so many questions that i cant find the answers anywhere.
when i call a non-static function "foo" in class A remotely from the client, how does the server know which instance of A has its foo invoked, if there are many gameobjects have A attached?
if the remotely called function "foo" has other function "foo2" calls inside foo's method body, what happens if that foo2 function is RPC/non-RPC in each case? what if foo is recursive?(that is foo2 is simply foo)
suppose foo2 is not a member function of class A, but rather a function of class B. Is there anything special that I should pay attention to?
Answer by Yokimato · Apr 11, 2013 at 05:51 PM
Hopefully I can answer all of these.
The second argument of the
Network.RPC
function, is the target. So what you put in there is how the server knows who to send it to. (source)foo2
can't be an RPC function unless you're making anotherNetwork.RPC
call to it. Once you've received an RPC in a RPC function, you can call any functions you want since it's a within an instance at that point.Again, from number 2, once you're in an RPC function...your have an instance so world's your oyster at that point. The another caveat is again calling RPC functions.
Per your comment, I think what you're having trouble with is understanding the abstraction Unity is doing on your behalf for synchronization in that your A
class will have N amount of instances, all of which have and maintain their own state and not state of each instance of A
.
i know RPC$$anonymous$$ode specifies which client/server has to make the function call. but im actually asking which instance of that class calls the function.
? The one that runs that code...your code should not be setup so that N number of class A is making the same RPC at the same time most likely. The point is to communicate when something happens. Client 0 of Class A takes damage, so Client 0 of Class A sends out the RPC (maybe class A has a TookDamage
function that does the RPC call). However, 10 seconds later, Client 4 of Class A now takes damage, so it then calls it's TookDamage
method and sends out the RPC.
Answer by whydoidoit · Apr 11, 2013 at 05:58 PM
So you call an RPC on a local object with a networkView and it calls the remote objects depending on how you address it - Everything, the Server, Others, or a specific NetworkPlayer.
If you continue to recursively call functions then again that will operate on whichever component you address the RPC call.
NetworkViews have a unique NetworkViewID that is the same on every computer and is used to address the actual object that will be called. You normally have at least one item in the scene that is allocated a scene NetworkViewID and can be used to communicate with all clients - this is owned by the server. Other objects will be owned by the thing that called AllocateNetworkViewID or instantiated it.
Your answer
Follow this Question
Related Questions
Acess server stuff by the client 0 Answers
how to properly clean up the NetworkView to prevent errors ? 0 Answers
Multiplayer Moving Bullet 1 Answer
How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers
Networking RPC sends to wrong target 0 Answers