Search Unity

Detecting when client is "connecting"

Discussion in 'Multiplayer' started by josephsaade, Sep 26, 2016.

  1. josephsaade

    josephsaade

    Joined:
    Apr 18, 2016
    Posts:
    46
    My short question:
    Is there a way the server detects if a client is currently in the progress of connecting?
    The "OnServerConnect" on NetworkManager is actually fire a few ms before the client joins so I am looking into something that is fired as soon as a connection is being established.


    Long Story:
    I am creating a multiplayer mode in our game which is a one-on-one board game.

    It is working fine with matchmaking, and we wrote a layer on top of NetworkManager so the player does not have to worry about being server or client, the game just tries to find a game to join, otherwise it hosts and waits till a client joins (like Clash Royale for example).

    Now we are adding a small feature where the server would wait for a few seconds and if no-one joins we are moving to another scene.

    The issue, is that sometimes, a client is actually joining the game, but we are not being notified and we had changed the scene. (OnServerConnect is not fired until the client fully joins).
     
  2. cjf93

    cjf93

    Joined:
    Apr 28, 2014
    Posts:
    14
    I think that there is no event on that, you are connected or not.
    Im working with the LLAPI and there is data, connect disconnect, broadcast and nothing event.
    I don't know how you make your "find a game to join" but you can have some control variables there to solve your problem.
     
  3. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    Not sure if this happens at the right time that you need it to or not, and it's not very elegant since I'm a rookie, but it works for what I'm up to.

    Code (CSharp):
    1. using UnityEngine.Networking;
    2.  
    3. public class LoadFlag : NetworkBehaviour
    4. {
    5.     [SyncVar]
    6.     public int NumberOfPlayers=0;
    7.  
    8.     [SyncVar]
    9.     public int NumberOfPlayersChanged=0;
    10.     void Update()
    11.     {
    12.         NumberOfPlayers = NetworkManager.singleton.numPlayers;
    13.         if(NumberOfPlayersChanged!= NumberOfPlayers)
    14.         {
    15.             NumberOfPlayersChanged = NumberOfPlayers;
    16.             if (isLocalPlayer)
    17.             {
    18.                 //DO STUFF
    19.             }
    20.         }
    21.     }
    22. }
    23.  
     
  4. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Rather than using 2 synced integers for detecting a change, you could use a function that one syncvar is hooked to
    Code (csharp):
    1. [SyncVar (hook = "playersChanged")]
    2. public int NumberOfPlayers = 0;
    3.  
    4. public void playersChanged(int playerCount) //This is only called when NumberOfPlayers changes
    5. {
    6.     if(isLocalPlayer)
    7.     {
    8.         //Do stuff
    9.     }
    10. }
    As for what the OP is requested, no, I don't think there's anything that is called between the time the connection is requested and when the client is fully connected
     
  5. g-hoot

    g-hoot

    Joined:
    Apr 18, 2015
    Posts:
    69
    @Vedrit Wasn't sure if the playercount was changed as soon as the players starts connecting, or after. Not to hijack this post, but in your simplified code above, would this line still be in "void Update()"?

    "NumberOfPlayers = NetworkManager.singleton.numPlayers;

    I'm just wondering if this will take up a lot of network. Thinking maybe better to have it check every 10 seconds or so instead of every frame.