ID:1137010
 
Thought I'd just share this snippit that adds a "players online" list to your stat panel.

It's just a really basic system, not optimized completely but considering it's a panel the player opens very rarely, the effect of optimizing it is negligible so I'm not even worrying about spending time on that.

If someone feels like writing an optimized version I'm happy to change this and credit the optimizations to you.

Also, there's no reason why you can't put this loop into your own process then output it to something flashier than a stat panel.

var/global
PLAYERS[0]
mob

Login()
PLAYERS.Add(src.name)

Logout()
PLAYERS.Remove(src.name)

Stat()
statpanel("World")
stat("Players Online:\n")

for(var/player in PLAYERS)
stat(player)
You could also add a reference to the player mob itself, thus giving you the ability to see more information about them than just their name
Like this:

var/global/mob/PLAYERS[0]

mob
Login()
PLAYERS.Add(src)
Logout()
PLAYERS.Remove(src)

Stat()
statpanel("World")
stat("Players Online:\n")

stat(PLAYERS)


This would list the players in the same fashion as stat(contents)
Isn't it
mob
Logout()
PLAYERS.Remove(src)


Or am I misundertanding something? (If so, my apologies.)
In response to Magicsofa
Magicsofa wrote:
Like this:

> var/global/mob/PLAYERS[0]
>
> mob
> Login()
> PLAYERS.Add(src)
> Logout()
> PLAYERS.Remove(src)
>
> Stat()
> statpanel("World")
> stat("Players Online:\n")
>
> stat(PLAYERS)
>

This would list the players in the same fashion as stat(contents)

Yeah thanks Magicsofa, you're absolutely right. That way you could then refer to src.name & src.level totally independently because you've got the mob to play around with.

The reason why I didn't do that was because right now I just wanted to see player names and so it's more efficient to just grab the names but for more than one property that's definitely a good way & probably the best way to do it.
In response to Magicsofa
Yeah Kboy is right, there's a small spelling mistake of Login() instead of Logout(). I changed it in my quote.
It's more efficient to use the += operator when you're adding single items to a list(same for -= vs Remove() for single items), opposed to Add(), which can take an arbitrary amount of arguments(items). In your case the performance difference is negligible I'm sure, though.
Or you could use a grid. This is primarily the hate for statpanels in me speaking, though.
In response to Red Hall Dev
Red Hall Dev wrote:
The reason why I didn't do that was because right now I just wanted to see player names and so it's more efficient to just grab the names but for more than one property that's definitely a good way & probably the best way to do it.

Yeah, if all you need is the names then you should not bother with referencing the whole object. Just depends on what you want the list to do.

Kboy33 wrote:
Or am I misundertanding something? (If so, my apologies.)

Whoops, thanks for pointing that out...edited the post

Magnum2k wrote:
It's more efficient to use the += operator when you're adding single items to a list(same for -= vs Remove() for single items), opposed to Add(), which can take an arbitrary amount of arguments(items). In your case the performance difference is negligible I'm sure, though.

Hmm, I didn't know that. Why is it more efficient? I tend to not use Add or Remove at all...what about adding things like this:

var/mylist = list()
mylist += list(1,2,3,4,5,6,7,8,9)

//or...

mylist += 1
mylist += 2
mylist += 3
mylist += 4
mylist += 5


In those cases, is it more efficient to use Add?
In response to Magicsofa
Indeed. The test I did consisted of numbers and strings only, in order. I didn't test atomic objects. I doubt testing atomic objects would make a difference, though. Add() probably uses an alternative algorithm internally to append multiple items to a list, while you're required to keep creating new lists when using the += operator, which is just essentially bulking up the initial list, creating lists within lists [...]
In response to Magnum2k
Wow, is "List += Item" really shorthand for "List = List + Item" (List + Item being a different /list object, of course)?

I figured it'd at least be something like "List[++List.len] = Item" instead. I wonder, is that faster?
I'm not sure...of course, generally one would not want to be adding and removing list items many times per tick, but in the event that you did want to it might make a big difference