Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What's wrong ? ( trying to use networkView.group )

Discussion in 'Multiplayer' started by meanruz, Jul 30, 2012.

  1. meanruz

    meanruz

    Joined:
    Feb 21, 2012
    Posts:
    7
    Hello,

    I'm trying to use networkView.group to clear some part of my code but I think I didn't understand how it's works .... (and the documentation is a little lite http://docs.unity3d.com/Documentation/ScriptReference/NetworkView-group.html )

    There's my Client code
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Client : MonoBehaviour {
    5.    
    6.     public GameObject prefab;
    7.    
    8.     int selectedGroup;
    9.    
    10.     bool connected = false;
    11.        
    12.     void OnGUI()
    13.     {
    14.         if( !connected )
    15.         {
    16.             if( GUILayout.Button( "Join Group 11" ) )
    17.             {
    18.                 selectedGroup = 11;
    19.                
    20.                 connectToServer();
    21.             }
    22.            
    23.             if (GUILayout.Button( "Join Group 12" ) )
    24.             {
    25.                 selectedGroup = 12;
    26.                
    27.                 connectToServer();
    28.             }
    29.         }
    30.         else
    31.         {
    32.             if( GUILayout.Button( "MOVE" ) )
    33.             {
    34.                 networkView.RPC( RPC_MOVE, RPCMode.Server, networkView.group );
    35.             }
    36.         }
    37.     }
    38.    
    39.     void connectToServer()
    40.     {
    41.         Debug.Log( "connectToServer" );
    42.         Network.Connect( "192.168.1.16", 25000, "secret" );
    43.     }
    44.    
    45.     void OnConnectedToServer()
    46.     {
    47.         connected = true;
    48.         networkView.group = selectedGroup;
    49.    
    50.         int forbidengroup;
    51.        
    52.         if( networkView.group == 11 )
    53.         {
    54.             forbidengroup = 12;
    55.         }
    56.         else
    57.         {
    58.             forbidengroup = 11;
    59.         }
    60.        
    61.         foreach (NetworkPlayer player in Network.connections)
    62.         {
    63.             Network.SetReceivingEnabled( player, forbidengroup, false );
    64.         }
    65.     }
    66.    
    67.     void OnDisconnectedFromServer( NetworkDisconnection mode )
    68.     {
    69.         connected = false;
    70.     }
    71.    
    72.     public const string RPC_MOVE = "rpcMove";
    73.     [RPC]
    74.     void rpcMove( int playergroup )
    75.     {
    76.         prefab.transform.Translate( Vector3.right * 2.0f * Time.deltaTime );
    77.     }
    78. }
    79.  
    And my server code
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Server : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.         Network.incomingPassword = "secret";
    9.        
    10.         Network.InitializeServer( 64, 25000, false );
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.    
    18.     public const string RPC_MOVE = "rpcMove";
    19.     [RPC]
    20.     void rpcMove( int playergroup, NetworkMessageInfo info )
    21.     {
    22.         networkView.group = playergroup;
    23.        
    24.         if( playergroup == 11 )
    25.         {
    26.             Network.SetReceivingEnabled( info.sender, 12, false );
    27.         }
    28.         else
    29.         {
    30.             Network.SetReceivingEnabled( info.sender, 11, false );
    31.         }
    32.        
    33.         networkView.RPC( RPC_MOVE, RPCMode.Others, playergroup );
    34.     }
    35.    
    36.     void OnPlayerConnected(NetworkPlayer player)
    37.     {
    38.         Debug.Log("Player connected from " + player.ipAddress + ":" + player.port);
    39.     }  
    40.    
    41.    
    42. }
    The idea is : if a client is in group 11, and send the RPC rpcMove, other client in the group 12 doesn't receive the rpc... but in fact all work like I never add group or set SetReceivingEnabled ...

    Any helps ?

    Thanks for any answers!