Raised This Month: $51 Target: $400
 12% 

How do you get the number of all alive players?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
cs1.7
Senior Member
Join Date: Oct 2008
Old 02-22-2010 , 11:38   How do you get the number of all alive players?
Reply With Quote #1

Hello,

how can you get the number of all alive players?
Spectators and players that still are not assigned yet, should be considered as dead, as well.
cs1.7 is offline
ILUSION
Senior Member
Join Date: Oct 2006
Location: Argentina
Old 02-22-2010 , 12:00   Re: How do you get the number of all alive players?
Reply With Quote #2

PHP Code:
public lol(id)
    
client_print(idprint_chat"Alive players: %d"alive_players())

alive_players()
{
    new 
players 0
    
    
for (new id 1id get_maxplayers(); id++)
    {
        if (!
is_user_alive(id)) return
        
        
players++
    }
    
    return 
players

ILUSION is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 02-22-2010 , 12:02   Re: How do you get the number of all alive players?
Reply With Quote #3

Either :

- using get_players() with "a" as flag.
- looping through all player and checking with is_user_alive()
__________________
Arkshine is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-22-2010 , 12:16   Re: How do you get the number of all alive players?
Reply With Quote #4

I'm not comparing get_players to looping through all separately but just providing a clean example.

PHP Code:
new playerplayersmax_players get_maxplayers( );
while( ++
player <= max_players )
     if( 
is_user_aliveplayer ) )
           
players++; 

Last edited by SnoW; 02-22-2010 at 12:20.
SnoW is offline
Send a message via MSN to SnoW
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 02-22-2010 , 12:17   Re: How do you get the number of all alive players?
Reply With Quote #5

PHP Code:
new g_MaxPlayers;
 
public 
plugin_init()
        
g_MaxPlayers get_maxplayers();
 
GetAlivePlayers()
{
        new 
Count;
 
        for (new 
Client 1Client <= g_MaxPlayersClient++)
                if (
is_user_alive(Client))
                        
Count++;
 
        return 
Count;

I'd say this is the fastest way.

@SnoW,
wouldn't that while() break if one of the players is not alive?
__________________

Last edited by hleV; 02-22-2010 at 12:25.
hleV is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-22-2010 , 12:22   Re: How do you get the number of all alive players?
Reply With Quote #6

Quote:
Originally Posted by hleV View Post
@SnoW,
wouldn't that while() break if one of the players is not alive?
I was editing before you said that! You must also start the loop from one instead of zero.
SnoW is offline
Send a message via MSN to SnoW
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 02-22-2010 , 12:23   Re: How do you get the number of all alive players?
Reply With Quote #7

Quote:
Originally Posted by SnoW View Post
You must also start the loop from one instead of zero.
Unless I do ++Client instead of Client++.

EDIT: Just tested. I'm wrong.
__________________
hleV is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-22-2010 , 12:28   Re: How do you get the number of all alive players?
Reply With Quote #8

Yea, I know. The statement ends before the condition is checked, so the pre and post operators differ only inside it.
SnoW is offline
Send a message via MSN to SnoW
cs1.7
Senior Member
Join Date: Oct 2008
Old 02-22-2010 , 12:32   Re: How do you get the number of all alive players?
Reply With Quote #9

Let's say someone leaves the server or joins the spectators. Will the code automatically update the alive players number accordingly? Or has the code to be executed at each event of joining /leaving/going to spec in order to get the number?


edit

In other words how can i get the number updated automatically each time someone dies, respawns, joins spectaror or leaves server?

Last edited by cs1.7; 02-22-2010 at 12:44. Reason: edit
cs1.7 is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 02-22-2010 , 12:53   Re: How do you get the number of all alive players?
Reply With Quote #10

How often are you going to check for alive players? If not too often, just perform GetAlivePlayers() (or make a function from SnoW's code) everytime you need to get alive players.
__________________
hleV is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:06.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode