Search Unity

Problem with RPCs on a network Instantiated object

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

  1. ping

    ping

    Joined:
    Jul 7, 2009
    Posts:
    4
    I'm running a web player/server game. Let's look at the code first and I'll show you the problem I'm running into:
    Server code:
    Code (csharp):
    1.  
    2. function Start () {
    3.     Network.InitializeServer(32, 25000);
    4. }
    5.  
    Client code:

    Script attached to the main camera or a dummy object:
    Code (csharp):
    1.  
    2. // "playerPrefab" is assigned to a prefab which is an empty object.
    3. var playerPrefab : Transform;
    4. function Start () {
    5.     Network.Connect("127.0.0.1", 25000);
    6. }
    7. function OnConnectedToServer(){
    8.     Network.Instantiate(playerPrefab, Vector3(0,0,0), Quaternion.identity, 0); 
    9. }
    10.  
    Script attached to "playerPrefab":
    Code (csharp):
    1.  
    2. var playername : String = "";
    3. function OnNetworkInstantiate(info : NetworkMessageInfo)
    4. {
    5.     if( networkView.isMine )
    6.     {
    7.     var name = "any";
    8.     networkView.RPC( "SetPlayerName", RPCMode.AllBuffered, name );
    9.     }
    10. }
    11.  
    12. @RPC
    13. function SetPlayerName( name : String )
    14. {
    15.     print("SetPlayerName:" + name);
    16.     gameObject.name = name;
    17. }
    18.  
    19.  
    Both client and server runs in windows. Server runs as a standalone app. One client runs in Unity, and another one runs in browser. When I run client from Unity, I see "SetPlayerName:any" in the console, then I run another client in browser, I'm expecting to see another line of "SetPlayerName:any" in the console, but what I get is "SetPlayerName:playerPrefab(clone)". It means "SetPlayerName" is not called. But it's a buffered RPC which SHOULD be called on every client, isn't it?

    Did I miss something in order for this to work?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    buffered RPCs must be generated by the server
    Clients can not request buffered RPC calls

    so what you would do is request the setname on the server and the server then actually sends this RPC around the players
     
  3. ping

    ping

    Joined:
    Jul 7, 2009
    Posts:
    4
    Thank you so much dreamora, I let server initiate buffered RPCs and it works!