Search Unity

using network groups

Discussion in 'Multiplayer' started by sigal, Jun 24, 2010.

  1. sigal

    sigal

    Joined:
    Apr 20, 2010
    Posts:
    9
    Hi

    I am trying to use unity's networking example
    and seperate the clients so each can select his own game, while keeping the multiplayering for each game

    (in the original project:
    when one player selects the "car" game
    the same "car" game is loaded on all players connected to the same server.
    I want players 1..5 to play "car" game while players 6..9 play "third-person" game)

    I was able to change the level loading (so now each player selects his game),
    and I changed the spawn scripts so for the third-person game I have:
    Code (csharp):
    1.  
    2. var playerPrefab : Transform;
    3.  
    4. function Awake(){
    5.     networkView.group = 5;
    6. }
    7.  
    8. function OnNetworkLoadedLevel ()
    9. {
    10.     Network.Instantiate(playerPrefab, transform.position, transform.rotation, 5);
    11. }
    12.  
    13. function OnPlayerDisconnected (player : NetworkPlayer)
    14. {
    15.     Debug.Log("Server destroying player");
    16.     Network.RemoveRPCs(player, 5);
    17.     Network.DestroyPlayerObjects(player);
    18. }
    19.  

    but I have 2 problems:

    p1. the prefab is instantiated on all connected screens,

    so if player1 instantiates a player on third-person game, while player2 runs the car game - the third-person prefab is instantiated also on player2's car game (you can see it on the scene view if you run the car game on unity).

    I don't get why the instantiation is not limited to the players who play the same game only (group 5)


    p2. the prefabs are not buffered so when a new player joins the game - all can see him but he can see only himself





    SO..
    I tried another way:
    Code (csharp):
    1.  
    2. var playerPrefab : Transform;
    3. private var isInstantiated : boolean = false;
    4.  
    5. function Awake(){
    6.     networkView.group = 5;
    7. }
    8.  
    9. function OnGUI () {
    10.     if (Network.isClient  !isInstantiated)
    11.         if (GUI.Button(new Rect(20,Screen.height-60, 90, 20),"SpawnPlayer"))
    12.         {
    13.                         networkView.RPC("SpawnPlayer", RPCMode.AllBuffered);
    14.             isInstantiated = true;
    15.         }
    16. }
    17.  
    18.  
    19. @RPC
    20. function SpawnPlayer () {
    21.     if(networkView.group == 5){
    22.         Instantiate(playerPrefab, transform.position, transform.rotation);
    23.     }
    24.     else{
    25.         Debug.Log("not in the group");
    26.     }
    27. }
    28.  
    29. function OnPlayerDisconnected (player : NetworkPlayer) {
    30.     Debug.Log("Cleaning up player " + player);
    31.     Network.RemoveRPCs(player, 5);
    32.     Network.DestroyPlayerObjects(player);
    33. }
    34.  
    but this way it acts as local Instantiate - player1's prefab is instantiated only on player1's screen..

    I don't get the grouping thing right, do I.. :? ?

    can anyone please enlight me..?
    thanks..
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Grouping in network views is commonly more for "priority channels". By default all goes through group 0 which is highest priority in sending.
    less important stuff would be sent through group 1 or 2 and so on.

    you can enable and disable groups at any time without problems.



    What you are potentially looking for is SetScope (assuming you use view syncronization) or just specifiy the target player if it is RPC
     
  3. sigal

    sigal

    Joined:
    Apr 20, 2010
    Posts:
    9
    I am using RPC only.

    And the target players are
    ALL the players connected to THIS level (and only this level)
    all players already in it and players that will connect later on.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you want to send it to all players in the current scene now or through the lifetime of the scene with RPCs you don't need groups etc at all.

    Thats a matter of using AllBuffered as rpc mode.

    and then on scene switch you would clear the whole RPC 0 group so the buffered messages are removed on the server too (the level prefixes help here too but can lead to annoying hickups)


    if you have multiple levels in one scene, you could seperate them through groups but the management there can become a nightmare and as mentioned, the groups are "channel priorities" so don't overdo this or the higher index "levels" will end with network connections that dry out due to non-importance
     
  5. sigal

    sigal

    Joined:
    Apr 20, 2010
    Posts:
    9
    dreamora - thanks for your help but i think you didn't get the point..

    I want players to be able to play different games while connected to the same server.
    if I use the AllBuffered - then players playing game1 can see players who play game2 - this is not what I want!!

    for example - if player1 plays the car game, while player 2 plays the castle game - there should NOT be a car instantiated on the castle screen.


    I know my first post was long, but I tried giving as much info to clear my point and show what I've tried so far - please do read it..