Raised This Month: $32 Target: $400
 8% 

Solved count alive players


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Watermelonnable
Member
Join Date: Feb 2017
Old 12-07-2019 , 11:49   count alive players
Reply With Quote #1

Hello. So I'm trying to do a workaround of the infinite round plugin by Arkshine. I want the round to end when there's only 1 player alive. So I'm hooking to the "ham killed" post to achieve this. There I'm trying to count the alive persons to execute my code:

PHP Code:
public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR);
    
pause("ac""infinite_rounds.amxx");
    
RegisterHamPlayer(Ham_Killed"Ham_Killed_post"1);
}

public 
Ham_Killed_post()
{
    new 
iPlayers[32], alivePlayers[32], rkWinnerName[32];
    
get_players(iPlayersalivePlayers"a");

    if (
alivePlayers <= 1) {
        
pause("ac""infinite_rounds.amxx");
        
get_user_name(iPlayers[0], rkWinnerNamecharsmax(rkWinnerName));
        
client_print_color(0print_team_default"%s ¡%s was the round winner"TTT_TAGrkWinnerName);
    }

But honestly I don't understand that well how get_players work. This is my concept of the function at the moments:

It saves the indicated players in the third argument (the flags) in the variable given as first argument (iPlayers) but I don't know what's the second argument. I thought it was the player count but the compiler is giving me an error saying that it must be indexed.

Any help here please?

Last edited by Watermelonnable; 12-07-2019 at 16:30.
Watermelonnable is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-07-2019 , 12:53   Re: count alive players
Reply With Quote #2

Bookmark this: http://www.amxmodx.org/api/

Here's what you need: http://www.amxmodx.org/api/amxmodx/get_players
__________________
Bugsy is offline
ZaX
Senior Member
Join Date: Jan 2015
Old 12-07-2019 , 12:57   Re: count alive players
Reply With Quote #3

You can get the alive counts by this

PHP Code:
GetAliveCount()
{
    new 
iAliveid
    
    
for (id 1id <= get_maxplayers(); id++)
    {
        if (
is_user_alive(id))
            
iAlive++
    }
    
    return 
iAlive;

and use
Code:
 if(GetAliveCount() == 1)
And if you want to know how you can use get_players, thats an example
PHP Code:
myfunction()
{
    new 
iPlayers[32], iNum
    
new id

    get_players
(iPlayersiNum "a"// Alive only

    
for(new 0iNumi++)
    {
        
id iPlayers[i]
        
set_user_health(id500)
    }


Last edited by ZaX; 12-07-2019 at 12:59.
ZaX is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-07-2019 , 13:13   Re: count alive players
Reply With Quote #4

ZaX, you should not use a constant native such as get_maxplayers() within the for-loop condition since it will get re-called for each iteration. Store the value in a variable and use that in the condition. I personally think it would be better to just use get_players() to get alive count.

PHP Code:
GetAliveCount()
{
    new 
iPlayers32 ], iNum;
    
    
get_players(iPlayersiNum "a")
    
    return 
iNum;

You can kill 2 birds with 1 stone:
PHP Code:
public Test() 
{
    new 
iPlayers32 ];
    new 
iAlive GetAlivePlayersiPlayers );
    
    
server_print"There are %d alive"iAlive );
}

GetAlivePlayersiPlayers32 ] )
{
    new 
iNum;
    
    
get_players(iPlayersiNum "a")
    
    return 
iNum;

__________________
Bugsy is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-07-2019 , 13:14   Re: count alive players
Reply With Quote #5

PHP Code:
new Alive get_playersnum_ex(GetPlayers_ExcludeAlive)

    if(
Alive 2
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-07-2019 , 13:31   Re: count alive players
Reply With Quote #6

Quote:
Originally Posted by iceeedr View Post
PHP Code:
new Alive get_playersnum_ex(GetPlayers_ExcludeAlive)

    if(
Alive 2
OP, you will need to be running AMX-X 1.9 to use the '_ex' version of get_players.
__________________
Bugsy is offline
Watermelonnable
Member
Join Date: Feb 2017
Old 12-07-2019 , 14:09   Re: count alive players
Reply With Quote #7

Thank you guys for your replies. Yes I checked the API documentation but maybe I'm kinda dumb so didn't understand it. I'm gonna try the iceeedr version since I'm using the latest amx version.

btw, shouldn't it be the GetPlayers_ExcludeDead flag?

EDIT: I'm sticking to get_players_ex since I also need to get the alive player's name

Last edited by Watermelonnable; 12-07-2019 at 14:15.
Watermelonnable is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 12-07-2019 , 15:43   Re: count alive players
Reply With Quote #8

Quote:
Originally Posted by Watermelonnable View Post
btw, shouldn't it be the GetPlayers_ExcludeDead flag?
Yup.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 12-08-2019 , 08:57   Re: count alive players
Reply With Quote #9

Quote:
Originally Posted by Watermelonnable View Post
btw, shouldn't it be the GetPlayers_ExcludeDead flag?

EDIT: I'm sticking to get_players_ex since I also need to get the alive player's name

Oops, Oops, error clicking
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/
iceeedr is offline
Send a message via Skype™ to iceeedr
Natsheh
Veteran Member
Join Date: Sep 2012
Old 12-08-2019 , 09:35   Re: count alive players
Reply With Quote #10

Why are you pausing the plugin ? Instead use the API (OnRoundEnd forward) and return 1 or plugin_handled to block the round from ending.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
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 15:24.


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