AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How can I find out the # of players on a team? (https://forums.alliedmods.net/showthread.php?t=60988)

I3lade203 09-18-2007 09:32

How can I find out the # of players on a team?
 
This is probably a very simple question; but i'm new so give me a break. What can I use to find out how many players are on a team?

_Tool_ 09-18-2007 11:23

Re: How can I find out the # of players on a team?
 
new players[32],num,teamnum=0;
get_players(players,num,"ce","team");
for(new i=0;i<num;i++) teamnum++;

/*flags:
"a" - Don't return dead players
"b" - Don't return alive players
"c" - Skip bots
"d" - Skip real players
"e" - Match with passed team
"f" - Match with part of name
"g" - Ignore case sensitivity
"h" - Skip HLTV
*/

I3lade203 09-18-2007 16:34

Re: How can I find out the # of players on a team?
 
Thanks, I figured I was going to have to use a for loop but I wasn't sure if there was already a pre-defined function that would just retrieve the number or not. Guess not, but I do have a question...

Here's what my code looks like so far:

Code:

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)   
    register_menucmd(register_menuid("Team_Select"), MENU_KEY_1 | MENU_KEY_2, "jointeam")   
    register_clcmd("jointeam 1", "jointeam")
    register_clcmd("jointeam 2", "jointeam")
    register_clcmd("jointeam 5", "jointeam")
   
}

Code:

public jointeam()
{
    new red_players, blue_players, x, numplayers
    numplayers = get_playersnum()
   
    for(x=0; x<=32; x++)
    {
   
       
       
        if( get_user_team(x) == 2 )
        {
            blue_players = blue_players + 1
            set_cvar_num("cr_demoman", -1)
       
        }else if (get_user_team(x) == 1 )
        {
            red_players = red_players +1
            set_cvar_num("cr_sniper", -1)
        }
   
    }
   
 
   
   
}

Thanks to you, I was able to get the number of players per team working. HOWEVER, the problem i'm now having is that the above code only exectues when a player CHANGES teams, rather then joins teams. So, if I join a server with the above code running, and I pick "Team 1" nothing will happen. If however after I spawn I join team 2, and then join team 1 again the "team 1" code will work. How can I fix this so that it'll execute the code whenever the player chooses the team, rather then switches to the team?

Zenith77 09-18-2007 18:34

Re: How can I find out the # of players on a team?
 
As a side note, get_players() and is kind of buggy when parsing out teams. It doesn't look like you should have any problems, but beware :O....

I3lade203 09-18-2007 18:46

Re: How can I find out the # of players on a team?
 
Quote:

Originally Posted by Zenith77 (Post 532764)
As a side note, get_players() and is kind of buggy when parsing out teams. It doesn't look like you should have any problems, but beware :O....

Well, I've tried my code with both get_players() and get_player_team -- however in either case when a user is initally joining a team after connecting to the server it doesn't execute the code within the for loop. However, if I join the game and change to the oppisite team it then DOES in fact execute the code.

How can I resolve this issue so that the code executes whenever a player joins a team, even if they weren't on one previously?

Wilson [29th ID] 09-18-2007 20:45

Re: How can I find out the # of players on a team?
 
It does execute the code within the loop. But your code does not do anything because it does not fall within the paramters of your if statements.

get_user_team(x) will not work on the player that just executed the command to join the team.

Think of the sequence of it:
1-Player joins server, with "no team assigned yet."
2-Player executes the "jointeam" command.
3-AMXX interrupts the command and executes code.
4-Player is then assigned to the team he joined.

Theoretically, you could block the person from joining the team, which is why AMXX interrupts and comes before, not after in this case.

So anyway,

Instead of doing a get_user_team() on the player joining the team, you should instead read the argument of the command being executed.

Code:
public cmd_jointeam(id) {    new args[8];    read_args( args, 7 );    new new_team = str_to_num( args ); }

In the above code block, the integer variable new_team will be equal to the team the client tried to join. Like I said, get_user_team() won't return anything for that client yet as the team has yet to be set.

I3lade203 09-19-2007 01:27

Re: How can I find out the # of players on a team?
 
Quote:

Originally Posted by Wilson [29th ID] (Post 532806)
It does execute the code within the loop. But your code does not do anything because it does not fall within the paramters of your if statements.

get_user_team(x) will not work on the player that just executed the command to join the team.

Think of the sequence of it:
1-Player joins server, with "no team assigned yet."
2-Player executes the "jointeam" command.
3-AMXX interrupts the command and executes code.
4-Player is then assigned to the team he joined.

Ah, that makes perfect sense - no wonder I was having so many issues with this. Thanks for explaining that. However..


Quote:

So anyway,

Instead of doing a get_user_team() on the player joining the team, you should instead read the argument of the command being executed.

Code:
public cmd_jointeam(id) { new args[8]; read_args( args, 7 ); new new_team = str_to_num( args ); }


In the above code block, the integer variable new_team will be equal to the team the client tried to join. Like I said, get_user_team() won't return anything for that client yet as the team has yet to be set.
In theory, I understand exactly what the code you've posted is supposed to do. I'm just having trouble interpreting it and integrating it into my existing code - could you show me how to take what you've written and place it in to what I had earlier to make the whole thing run altogether?

Wilson [29th ID] 09-19-2007 03:46

Re: How can I find out the # of players on a team?
 
Code:
public jointeam() {     new red_players, blue_players, x, numplayers     args[8], numplayers = get_playersnum()     read_args( args, 7 );     new cmd_team = str_to_num( args );     if( cmd_team == 2 )     {             blue_players = blue_players + 1             set_cvar_num("cr_demoman", -1)     }     else if( cmd_team == 1 )     {             red_players = red_players +1             set_cvar_num("cr_sniper", -1)     }     // For everyone else, loop     // (it will skip the above player since get_user_team() == 0 for him)     for(x=0; x<=32; x++)     {         if( get_user_team(x) == 2 )         {             blue_players = blue_players + 1             set_cvar_num("cr_demoman", -1)                 }else if (get_user_team(x) == 1 )         {             red_players = red_players +1             set_cvar_num("cr_sniper", -1)         }         }               }

This basically does the stuffs for the player doing the command, and then does it in addition for everyone else. This just an example and obviously not the most efficient way. But this way you get the idea.

I3lade203 09-19-2007 08:23

Re: How can I find out the # of players on a team?
 
OH, I see now. Thanks! I'm still learning, so most efficient way or not, if it works and I learned a new way do so something, it's successfully. :up: Thanks for the help, I'll try the code out when I get home later today and see what happens.


All times are GMT -4. The time now is 16:08.

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