How to implement a Room Mode Multiplayer Game code structure.

I am trying to seperate players into different room by setting them a difenent networView.group id just like this:

    function OnPlayerConnected(p:NetworkPlayer) {
      var n:int = Network.connections.length;
      if ((n%2) == 1) { // simply divide into 2 room(room id is 3 and 4).
        Network.SetReceivingEnabled(p, 4, false);
        Network.SetSendingEnabled(p, 4, false);
        networkView.RPC("SetGroup", player, 3);
        // the odd player will be in room 3 and not receive room 4's message
      } else {
        Network.SetReceivingEnabled(p, 3, false);
        Network.SetSendingEnabled(p, 3, false);
        networkView.RPC("SetGroup", player, 4);
        // the even player will be in room 4 and not receive room 3's message
      }
    }
    @RPC
    function SetGroup(group:int) {
      myRoomGroup = group;
    }
    function OnGUI() { // client
      if (Network.isClient) {
        if (GUILayout.Button("spawn")) {
          Network.Instantiate(prefabTest, Vector3.zero, Quaternion.identity, myRoomGroup);
        }
      }
    }

Ok, that is the main code above, and the situation is like this:
There are Server, Client1, Client2, Client3 for this test.

    Server.start();
    Client1.connect(); // in room 3
    Client2.connect(); // in room 4
    Client1.click("spawn");
      // PrefabTest.spawned at Client1 and at Server.
      // PrefabTest doing not instantiated in Client2 is what I want as I just want
      // seperate Client1 and Client2 to different room. And there meesage won't send
      // to each or received from, so now it runs well.
    Client2.click("spawn");
      // PrefabTest.spawned at Client2 and at Server.
      // So this is also right for my purpose.
    Client3.connect(); // in room 3
      // Client1.prefabTest is synchronized to Client3(yes, i want it);
      // Client2.prefabTest is also synchronized to Client3(oh no...);

The problem is that the Client2.prefabTest should not be instantiated to Client3 as that is not I want. And the reason is that the buffered-RPC(Network.Instaiated) is invoked when a player connected and before Server.OnPlayerOnnected is called, so my code is a little later than the auto-synchronization, so the problem occurred.

As trying useing the NetworkView with a same group id insteading of the Network.Instantiate for synchonizing GameObject, the Synchronization is always lanuched from the server to client since the networkView.owner(0) is alway the server.

I have try thinking of many way to implement a room mode for multiplayer game by using Network System, but I think I am going in a wrong way (:3|L)_.
So what is the correct way to implement Room mode using unity’s Network System?
Many thank for any replay :slight_smile: .

by smallkhb
2014-03-23

The correct answer is sadly that networkgroups are not intended for spitting a game like this, and this doesn’t work properly (as you have noticed), and there is no real way around this with the default unity networking.