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

Improving NetManager with Team-selection - any possibility?

Discussion in 'Multiplayer' started by Justice0Juic3, Mar 12, 2016.

  1. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    188
    Just what the title says. I've seen several "modifications" on the NetworkManager inside the documentation but I don't understand a way doing the following.

    Here's a drawing I made of the concept:


    *Assume that no matter if we connect as Client or start as Host, the player will always have to select a team before he can start playing.

    This is a rather important thing I need to learn, so if anybody could teach me... I will give them 50% of the cookies from the cookie-jar. :p
     
  2. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    There are probably a lot of ways to achieve this, but perhaps the easiest way would be to use the NetworkLobbyManager to create a "lobby" where players pick their team before entering the game scene.

    You could download the Network Game Lobby project from the asset store (http://u3d.as/i1Q) and play with it to get a desired result.

    Hope this helps!
     
  3. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    188
    This might be the final step, because when I should be able to test the core-mechanics of the game quickly.

    However this helped me, I wondered if I'd need two Lobby Player Prefabs or two Game Player Prefabs.
    The reason is simply because I'm going to use separate tags for both Players: "PlayerRed" and "PlayerBlue".
    Should I modify the LobbyManager like so?
     
  4. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    The way the LobbyManager works is that it is designed to override the default behaviour of the NetworkManager to add a NetworkLobbyPlayer instead of the game player object when the client connects. The NetworkLobbyPlayer can be anything but is most likely an UI component for each player in the lobby.

    You could just inherit from the NetworkLobbyPlayer component and create something like this:

    Code (CSharp):
    1.  
    2. public class MyLobbyPlayer : NetworkLobbyPlayer{
    3.     // Team
    4.     [SyncVar]
    5.     int team
    6.  
    7.     // Buttons
    8.     public Button redTeam
    9.     public Button blueTeam
    10.  
    11.     public override void OnStartClient(){
    12.         base.OnStartClient();
    13.  
    14.         // Initially hide buttons for clients
    15.         redTeam.gameObject.setActive(false);
    16.         blueTeam.gameObject.setActive(false);
    17.     }
    18.  
    19.     public override void OnStartLocalClient(){
    20.          base.OnStartLocalClient();
    21.  
    22.         // When client is set as local, enable buttons
    23.         redTeam.gameObject.setActive(false);
    24.         blueTeam.gameObject.setActive(false);
    25.  
    26.         // Add listeners
    27.         redTeam.onClick.RemoveAllListeners();
    28.         redTeam.onClick.AddListener(OnClickRed);
    29.  
    30.         blueTeam.onClick.RemoveAllListeners();
    31.         blueTeam.onClick.AddListener(OnClickBlue);
    32.     }
    33.  
    34.     public void OnClickRed(){
    35.          // Clicked red team (team nr 0)
    36.          CmdSelectTeam(0);
    37.     }
    38.  
    39.     public void OnClickBlue(){
    40.          // Clicked blueteam (team nr 1)
    41.          CmdSelectTeam(1);
    42.     }
    43.  
    44.     [Command]
    45.     public void CmdSelectTeam(int teamIndex){
    46.         // Set team of player on the server.
    47.         team = teamIndex;
    48.     }
    49.  
    50. }
    51.  
    Then, when you override the behaviour of switching to your game player object, you can just store the value of of your "team" somewhere in your game player object and go from there.

    Note that your Lobby Player object has to be set as Local Player Authority in their NetworkIdentity component.

    Let me know if this works.
     
    Justice0Juic3 likes this.
  5. Justice0Juic3

    Justice0Juic3

    Joined:
    Apr 29, 2013
    Posts:
    188
    @sovium - I am sure that above code and explaining works like it should be.
    I assume you're talking about:

    That's exactly what I'm up to.
    Now that I know that the last step remains here, I will go back to this thread once all core-mechanics of the game are actually finished.

    You really helped me, thank you.
     
    sovium likes this.
  6. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
    I know this is uber late but just want to let it be known that this post is what i have needed for the last few days. I have spent easily 20 or 30 hours trying to setup teams which is admittedly depressing and doing a ton of attempts at using syncvars and commands and clientrpc calls this solution is what I needed... so thank you so much.

    At the same time i have this weird situation now where when exiting the scene the environment disappears but my character remains active... I don't mind it at all since its kinda funny but if anyone is still around I would love to hear thoughts.

    I am using basically the recommended script here in conjunction with network lobby manager basic setup.

    Thanks
     
  7. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    I used the lobby asset (and NetworkLobbyManger) for more than a year in uMOBA. It's full of bugs, don't bother.
    You will save yourself a lot of headaches if you simply inherit from NetworkManager to make a simply lobby yourself. Make a list of player connections with a ready flag, then change scene + spawn prefabs when everyone is ready.

    Your scene switching problem might be related to a UNET bug too. Scene switching fails about 30% of the time (depending on how fast your computer loads the scene). There is no way around it unless you use a fixed version like Mirror (see my signature).