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.

  1. 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?
  2. 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)
  3. 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?

Hopefully I can answer all of these.

  1. 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)
  2. foo2 can’t be an RPC function unless you’re making another Network.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.
  3. 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.

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.