Search Unity

How client call a function on server through RPC?

Discussion in 'Multiplayer' started by ping, Jul 7, 2009.

  1. ping

    ping

    Joined:
    Jul 7, 2009
    Posts:
    4
    Hi there,
    This is my first question about networking. The idea is after client connecting to server, it calls a "login" function on server and server sends back avatar information.
    From client site, after connection, it sends the following RPC request:
    Code (csharp):
    1.  
    2. function OnConnectedToServer() {    networkView.RPC("ClientLogin",RPCMode.Server,playerName,playerAvatarId);
    3. }
    4.  
    On server side, the RPC function is defined as:
    Code (csharp):
    1.  
    2. @RPC
    3. function ClientLogin (playername : String, avatarid: int)
    4. {
    5.     var modelname : String = "playerPrefab";
    6.     var pos : Vector3 = Vector3(0,0,0);
    7.     //networkView.RPC("Spawn", info.sender, modelname, pos);
    8.    
    9.     print(playername + " logged in with avatar " + avatarid);
    10. }
    11.  
    The function "ClientLogin" never get called, and I got these errors(on server side):
    View ID SceneID: 2 Level Prefix:0 not found during lookup. Strange behaviour may occur

    Do I need to setup networkView id for client/server communication? Or I have to use Network.Instantiate?
    Which means the RPC funtion doesn't function by itself, it must adhere to a GameObject instantiated over the network?

    Need your advice, thanks!
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    RPC functions need to be in a component thats placed on an object with an existant network view to work.

    Also, although I guess thats known, don't forget to include the component with the server side rpc in the client build in case you have distinct projects. Otherwise the RPC id for the function is not known and unless that has changed with 2.5, it won't work
     
  3. ping

    ping

    Joined:
    Jul 7, 2009
    Posts:
    4
    Thanks dreamora, I finally made it work by instantiating a communication prefab from server side and all RPC calls to server go through the networkview attached to this prefab
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Great :)