Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Getting number of connected players when you are the server.

Discussion in 'Multiplayer' started by EducaSoft, Apr 19, 2008.

  1. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Hi,

    If I poll the hostlist from the masterserver, then I can easily retrieve the information about all available servers and their name + connected players and max connections.

    However.

    Once I let my program BE the server, I would like to be able to see how many clients are currently connected to me.

    Is there a way to ask unity how many clients are connected to me (as a server?).

    I'd like to have a message showing "There are currently X players connected to you"

    But I don't really poll the hostlist for that. It is rather local.


    Any idea somebody ?


    Kind regards,

    Bart
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Code (csharp):
    1.  
    2. var numOfConnectedPlayers = Network.connections.Length;
    3.  
    -Jeremy
     
    PatrickWoo likes this.
  3. EducaSoft

    EducaSoft

    Joined:
    Sep 9, 2007
    Posts:
    650
    Ah thanks,

    Didn't think of going to look there...
     
  4. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    Network.connections.length doesn't seem to work in C#, how would you get around it? At least I can't seem to get it working in Leepos awesome networking tutorial.

    Thank you!
     
  5. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    301
    It doesn't work in C#? Unusual. Please post some code.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    the length of an array is the value in Length in C# / .NET actually, js just has a deprecated alias (not sure its still there) for it and thus can use length.
     
  7. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    Code (csharp):
    1. GUILayout.Label("Connections: " + Network.connections.length);
    2.  
    or even a line like:
    Code (csharp):
    1. if (Network.connections.length >= 1)

    error CS0117: `UnityEngine.NetworkPlayer[]' does not contain a definition for `length'


    The above line of code works fine in JS.

    Thank you!

    David
     
  8. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    In C# it's capital L length. Just one of those quirks.
     
  9. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    Thank you appreciate it!