Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Network RPC

Discussion in 'Multiplayer' started by gazwi86, Mar 26, 2008.

  1. gazwi86

    gazwi86

    Joined:
    Jan 25, 2008
    Posts:
    21
    Right, i need to admit im a noobie. this is my first networked game and i have been working from the network example code. Im trying to destroy the visible player on a remote view whaen the player changes level as it doesnt already. my code is as follows:


    Code (csharp):
    1.  
    2. var supportedNetworkLevels : String[] = [ "mylevel" ];
    3. var disconnectedLevel : String = "loader";
    4.  
    5. // Keep track of the last level prefix (increment each time a new level loads)
    6. private var lastLevelPrefix = 0;
    7.  
    8. function Awake ()
    9. {
    10.     // Network level loading is done in a seperate channel.
    11.     DontDestroyOnLoad(this);
    12.     networkView.group = 1;
    13.     Application.LoadLevel(disconnectedLevel);
    14. }
    15.  
    16. function OnGUI ()
    17. {
    18.     // When network is running (server or client) then display the levels
    19.     // configured in the supportedNetworkLevels array and allow them to be loaded
    20.     // at the push of a button
    21.     if (Network.peerType != NetworkPeerType.Disconnected)
    22.     {
    23.         GUILayout.BeginArea(Rect(0, Screen.height - 30, Screen.width, 30));
    24.         GUILayout.BeginHorizontal();
    25.        
    26.         for (var level in supportedNetworkLevels)
    27.         {
    28.             if (GUILayout.Button(level))
    29.             {
    30.                 // Make sure no old RPC calls are buffered and then send load level command
    31.                 Network.RemoveRPCsInGroup(1);
    32.                 // Load level with incremented level prefix (for view IDs)
    33.                
    34.                 networkView.RPC( "LoadLevel", RPCMode.AllBuffered, level, lastLevelPrefix + 1);
    35.                 networkView.RPC( "DestroyPlayer", RPCMode.Others, NetworkViewID, NetworkPlayer);
    36.             }
    37.         }
    38.         GUILayout.FlexibleSpace();
    39.         GUILayout.EndHorizontal();
    40.         GUILayout.EndArea();
    41.     }
    42. }
    43.  
    44. @RPC
    45. function LoadLevel (level : String, levelPrefix : int)
    46. {
    47.     Debug.Log("Loading level " + level + " with prefix " + levelPrefix);
    48.     lastLevelPrefix = levelPrefix;
    49.  
    50.  
    51.  
    52.     // There is no reason to send any more data over the network on the default channel,
    53.     // because we are about to load the level, thus all those objects will get deleted anyway
    54.     Network.SetSendingEnabled(0, false);
    55.  
    56.     // We need to stop receiving because first the level must be loaded.
    57.     // Once the level is loaded, RPC's and other state update attached to objects in the level are allowed to fire
    58.     Network.isMessageQueueRunning = false;
    59.        
    60.     // All network views loaded from a level will get a prefix into their NetworkViewID.
    61.     // This will prevent old updates from clients leaking into a newly created scene.
    62.     Network.SetLevelPrefix(levelPrefix);
    63.     Application.LoadLevel(level);
    64.     yield;
    65.     yield;
    66.    
    67.     // Allow receiving data again
    68.     Network.isMessageQueueRunning = true;
    69.     // Now the level has been loaded and we can start sending out data
    70.     Network.SetSendingEnabled(0, true);
    71.  
    72.     // Notify our objects that the level and the network is ready
    73.     for (var go in FindObjectsOfType(GameObject))
    74.         go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
    75. }
    76.  
    77. @RPC
    78. function DestroyPlayer (NetworkViewID : int, NetworkPlayer : Transform){
    79.     Network.Destroy(GetComponent(NetworkView).viewID);
    80.  
    81.    
    82. }
    83.  
    84. function OnDisconnectedFromServer ()
    85. {
    86.     Application.LoadLevel(disconnectedLevel);
    87. }
    88.  
    89. @script RequireComponent(NetworkView)
    90.  
    But that doesnt seem to work. Ive really been struggleing to get my head around all this so if you could explain what ive done wrong i would be most grateful.