Jump to content

[Help] Getting a list of player names on the server to match to IDs


douglasg14b

Recommended Posts

I am trying to sort out how to get a list of player names on the server so I can match a name to an id to instantiate the player object Player(id).

 

I have found Galaxy():getPlayerNames(), however it returns a string, not a list, of player names separated by spaces (I think spaces??). I cannot split this string as there are players with spaces in their names.

 

You cannot use Galaxy():findFaction() for this as player names can change in steam (per documentation)

 

How can I get a list of names, server-side only, so I can match a player name up to a player ID?

 

Edit: Found Server():getPlayers() however it always returns nil.  Server():getOnlinePlayers() seems to return a userdata object, but I can't find documentation on it's properties or functions.

Link to comment
Share on other sites

I have found Galaxy():getPlayerNames(), however it returns a string, not a list, of player names separated by spaces (I think spaces??). I cannot split this string as there are players with spaces in their names.

That's an issue if it returns it separated with spaces. Should be submitted as bug, I would say.

 

You cannot use Galaxy():findFaction() for this as player names can change in steam.

Let's pretend for a moment that it will work with a player name (it used to; it doesn't now). I don't see a problem with that. Player is bound to a name for single session on server (heck, even for a game session as it doesn't refresh). So as long as one doesn't want do remember player name (why would you want that in the first place? use index/id instead then get player name with it from faction object) it should be a good solution.

 

Found Server():getPlayers() however it always returns nil.  Server():getOnlinePlayers() seems to return a userdata object, but I can't find documentation on it's properties or functions.

It is written in documentation what it returns: function Player... getOnlinePlayers(), so basically:

local players = {Server():getOnlinePlayers()} -- table of Players (Player object)

If getPlayers doesn't work properly try playing with it around as much as you can and also submit it as a bug if you are sure it doesn't work.

 

By the way it would be nice to have meta __type set up for various userdata (there are some projects that actually did that and it is quite lovely to deal with), but I guess that's just my small wish.

Link to comment
Share on other sites

 

Let's pretend for a moment that it will work with a player name (it used to; it doesn't now). I don't see a problem with that. Player is bound to a name for single session on server (heck, even for a game session as it doesn't refresh). So as long as one doesn't want do remember player name (why would you want that in the first place? use index/id instead then get player name with it from faction object) it should be a good solution.

 

It is written in documentation what it returns: function Player... getOnlinePlayers(), so basically:

local players = {Server():getOnlinePlayers()} -- table of players

If getPlayers doesn't work properly try playing with it around as much as you can and also submit it as a bug.

 

Hey Aki.

 

Too bad about the faction name not being effective for getting the player name anymore, I was trying it with no success earlier, it would have been a good workaround. Regardless of how it could be used, it can't be used.

 

Server():getOnlinePlayers() does not return a table. I can't use pairs on it, the error I get is: bad argument #1 to 'pairs' (table expected, got userdata). If I try and index it like an array Server():getOnlinePlayers()[0], I get an error saying: property not found or not readable: Player.0 which implies this is a player object? But none of the properties you would use on a Player object seem to return anything. (I am not very familiar with Lua as I am with other languages)

 

As for playing with Server():getPlayers(), it returns what appears to be nil so there isn't much to play with sadly :/

 

Should I submit a bug for Server():getOnlinePlayers() as well as Server():getPlayers() and Galaxy():getPlayerNames()? Or is there some way to further poke at Server():getOnlinePlayers()? Would the bug report forum be appropriate for this?

Link to comment
Share on other sites

Hey, I've been playing around a lot with those :

Server():getOnlinePlayers()

Server():getPlayers()

Galaxy():getPlayerNames()

Etc...

 

I recall there was some weird behaviour on some of them or it was just not working.

Currently at work, I'll let you know this evening which one I am using and how.

 

 

 

 

 

Link to comment
Share on other sites

Server():getOnlinePlayers() does not return a table. I can't use pairs on it, the error I get is: bad argument #1 to 'pairs' (table expected, got userdata). If I try and index it like an array Server():getOnlinePlayers()[0], I get an error saying: property not found or not readable: Player.0 which implies this is a player object? But none of the properties you would use on a Player object seem to return anything. (I am not very familiar with Lua as I am with other languages)

Did I say it returns table? Read again what I wrote: it returns Player.... I even made that bold. Look at the code I gave you and notice {}. If you catch vararg without {} into a variable (local players = Server():getOnlinePlayers()) then you will get only first Player object, which is of type "userdata".

Link to comment
Share on other sites

 

Did I say it returns table? Read again what I wrote: it returns Player.... I even made that bold. Look at the code I gave you and notice {}. If you catch vararg without {} into a variable (local players = Server():getOnlinePlayers()) then you will get only first Player object, which is of type "userdata".

 

Why are you being so abrasive/condescending? That does not seem warranted at all, especially coming from a moderator. You said that it is a --table of players, so I interpreted that as it being a table in your context.

 

I mentioned that I am not familiar with Lua to set a precident so you could hopefully forgive some ignorance of code structure, syntax, and common language structures/definitions. I was not aware that the squiggly brackets surrounding the functioncall would change what it returns, I come from a C# and JavaScript environment and that is not a behavior I am familiar with.

 

That being said, I will try this again when I am home, thank you.

Link to comment
Share on other sites

Why are you being so abrasive/condescending? That does not seem warranted at all, especially coming from a moderator. You said that it is a --table of players, so I interpreted that as it being a table in your context.

Simply because I'm abrasive but I never meant to be condescending. I'm sorry if you see it that way. Yeah, I said it is a "table" but in which context? The whole explanation is in my two previous posts, also something to clear it up below.

 

I mentioned that I am not familiar with Lua to set a precident so you could hopefully forgive some ignorance of code structure, syntax, and common language structures/definitions. I was not aware that the squiggly brackets surrounding the functioncall would change what it returns, I come from a C# and JavaScript environment and that is not a behavior I am familiar with.

 

That being said, I will try this again when I am home, thank you.

I think it is somehow similar to Params in C# (in terms of function parameters), if that helps you. Also brackets don't change what function returns. You could see it that way:

local function foo()

  return 1, 2, 3

end

local a = foo() -- a == 1; 2 and 3 are gone

local a,b,c = foo() -- a == 1, b == 2, c == 3

local t = {foo()} -- t is a table: {1, 2, 3}

local t = {0, foo()} -- t is a table: {0, 1, 2, 3}

-- next one is tricky:

local t = {foo(), 4} -- t is a table: {1, 4}

 

I think this code should cover most of the cases.

Link to comment
Share on other sites

Simply because I'm abrasive but I never meant to be condescending. I'm sorry if you see it that way. Yeah, I said it is a "table" but in which context? The whole explanation is in my two previous posts, also something to clear it up below.

 

All good, looks like it was just a misunderstanding. I come from a pretty professional environment, so I'm often taken aback by abrasiveness if I'm not expecting it.

 

I think it is somehow similar to Params in C# (in terms of function parameters), if that helps you. Also brackets don't change what function returns. You could see it that way:

local function foo()

  return 1, 2, 3

end

local a = foo() -- a == 1; 2 and 3 are gone

local a,b,c = foo() -- a == 1, b == 2, c == 3

local t = {foo()} -- t is a table: {1, 2, 3}

local t = {0, foo()} -- t is a table: {0, 1, 2, 3}

-- next one is tricky:

local t = {foo(), 4} -- t is a table: {1, 4}

 

I think this code should cover most of the cases.

 

I see. For clarity the Params in C# just lets you pass a variable number of parameters or arguments into a function, I think it's similar to luas ... in a function call. In my area of familiarity you would never be able to do return 1,2,3,4 unless the language would automatically create an array or some sort of object like [1,2,3,4] in which case you would get an array back and would index it or loop through it for your value.

 

The behavior you just illustrated is novel for me. I'll let you know the results when I'm back home.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...