How RPC works?

Hi
I have 3 scripts.

1st for Slient/Server selection

public class AeEngine : MonoBehaviour {
...

public void OnGUI() {
if (GUILayout.Button ("Server")) {
gameObject.AddComponent(typeof(AeServer));	
}
if (GUILayout.Button ("Client")) {
gameObject.AddComponent(typeof(Client));	
}}

..
}

Client Script

public class AeClient : MonoBehaviour
{
void Awake() {
		gameObject.name = "Client";
		gameObject.AddComponent(typeof(NetworkView));	
		networkView.group = 1;
		networkView.observed = null;
		networkView.stateSynchronization = NetworkStateSynchronization .Off;
}
...

public void OnConnectedToServer() {
networkView.RPC( "Login", RPCMode.Server, login, password);
}

[RPC]
public void InstantiateLocalPlayer(...){...}
}

Server Script

public class AeServer : MonoBehaviour {
void Awake() {
		gameObject.name = "InstanceServer";
		
		gameObject.AddComponent(typeof(NetworkView));
		networkView.group = 1;
		networkView.observed = null;
		networkView.stateSynchronization = NetworkStateSynchronization .Off;
}
...
[RPC]
public void Login(string login, string password, NetworkMessageInfo msgInfo) {
		//get player from database
		AePlayer player = storage.GetPlayerByLogin(login);
		if(player != null) {
			if (player.password.Equals(password)) {
				// for test only
				Vector3 position = Vector3.one;
				Quaternion rotation = Quaternion.identity;
				networkView.RPC("InstantiateLocalPlayer", null, position, rotation);
			}
		}
	}
}

Result is

RPC call failed because the function 'Login' does not exist in the any script attached to'Client'
UnityEngine.NetworkView:Internal_RPC_Target(NetworkView, String, NetworkPlayer, Object[])
UnityEngine.NetworkView:Internal_RPC_Target(NetworkView, String, NetworkPlayer, Object[])
UnityEngine.NetworkView:RPC(String, NetworkPlayer, Object[]) (at C:\builds\unity-trunk\unity\Runtime\Export\Generated\Networking.cs:185)
AeClient:OnConnectedToServer() (at Assets\Scripts\Engine\AeClient.cs:65)

[..\..\Runtime\Network\PackMonoRPC.cpp line 169]

:?: 1. I don’t understand why Unity trying to run “Login” method on my “Client”.
:?: 2. Am I understand correctly that

networkView.RPC( "Login", RPCMode.Server, login, password);

and

networkView.RPC( "Login", Network.connections[0], login, password);

are equivalent on the Client side?

Any idea? :cry:
Maybe someone can provide sequence diagram for RPC?

RPC functions must be present in client and server.
If either side does not know it, it can’t be used as it won’t receive an RPC ID to be sent upon the rpc call

Is the RPC functions must be the same on client and server? Or just have same name?

Actually I don’t want to have server specific implementation or plugins (dlls) on the client side.

I am just starting with networking in Unity and the same question arose:
When using RPC; do you have the client and server running the exact same script? Would this mean a client has all the server logic too?

the function must be known to both sides, yes. otherwise there won’t be a corresponding rpc on the other end to call

Thanks for the answer.

I found you can leave the “Serverside RPC methods” empty on the client. This way there is no server logic code on the client, which sounds wise.

I have to say, coding in a network environment requires a very different point of view.

Understood. Thanks!

BTW For now I am using delegate. Like this…
And I fond it very usefull.

public delegate void LoginDelegate(string login, string password, NetworkMessageInfo msgInfo);
...
-------------------------------

class RPCFacade {
...
public LoginDelegate loginDelegate;
...
[RPC]
public void Login(string login, string password, NetworkMessageInfo msgInfo){
		loginDelegate(login, password, msgInfo);
}
...
-------------------------------
class Server {
...
public void RPCSubscribe() {
		RPCFacade.Instance.loginDelegate += this.Login;
	}
}

But how about

networkView.RPC( “Login”, RPCMode.Server, login, password);

and

networkView.RPC( “Login”, Network.connections[0], login, password);

are they equivalent?

Hi, sorry to open an old thread but i was wondering if anyone had any insight to how the RPC’s actually work or invoking a method over the network. haven’t been able to successfully do this using sockets.