+ Reply to Thread
Results 1 to 3 of 3

Thread: [AS3] userManager.users.length always returns 0

Hybrid View

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    [AS3] userManager.users.length always returns 0

    Hi,

    I'm using ES5 with AS3 for my game and I want to assign user names based on the number of users currently connected (e.g. user0, user1, user2). I assumed that using userManager.users.length would return the current number of users connected therefore creating a unique username for each user. For some reason it always returns 0.

    I'm using the code inside the onConnectionResponse function so a connection to the server should have been established and the user list should have been retrieved surely?

    Thanks
    Last edited by monkfish101; 02-27-2012 at 12:33 AM. Reason: Forgot to specify the version of Electroserver I am using

  2. #2
    Administrator tcarr's Avatar
    Join Date
    Dec 2007
    Posts
    7,214
    Thanks
    80
    Thanked 1,087 Times in 1,076 Posts
    First, the list of users in UserManager is only those users who are either your buddies or who are in the same room you are. Second, before you log in, you can't get the usernames of ANY of the users who are logged on.

    If you want to assign usernames instead of either allowing users to choose their own or just assigning randomly, the best way to do it is to have a LoginEventHandler assign the usernames. The LoginEventHandler can have an AtomicInteger variable and then use getAndIncrement each time a user tries to log in. This will give you usernames in the order of login. It still won't be exactly what you want because if there are 75 users online (so there's user70, user71, ..., user75) then users 1 through 5 log out, you do NOT want the next user to log on to be user 70 - that's already taken.

    The code example named LoginEventHandler just assigns usernames randomly, but it would be easy to have it use an AtomicInteger variable for keeping track of the number, if you want to not have the usernames randomly assigned. Random works well, and if you have concerns about collisions, you can always have a loop that tries 10 times (in the login event handler). A login event handler can check whether a username is taken, using getApi().isUserLoggedIn(username).
    Teresa Carrigan
    Senior Engineer
    Electrotank, Inc.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok, cool, I didn't know about the isUserLoggedIn() function - sounds like exactly what I need. Thanks for the quick reply

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts