AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   execute a comand on a team (https://forums.alliedmods.net/showthread.php?t=86773)

rufee 03-02-2009 11:14

execute a comand on a team
 
how to execute a comand on the whole team ?
for example slaying CT

Bugsy 03-02-2009 11:17

Re: execute a comand on a team
 
Quote:

Originally Posted by rufee (Post 772356)
how to execute a comand on the whole team ?
for example slaying CT

http://www.amxmodx.org/funcwiki.php?go=func&id=174

Then use a for loop for all players

PHP Code:

new iPlayers[32];
new 
iNum;
get_playersiPlayers iNum ,"e" ,"CT" );
//get_players( iPlayers , iNum  ,"e" ,"TERRORIST" )

for( new 0iNum i++)
{
    
//slay iPlayers[i];



SnoW 03-02-2009 13:13

Re: execute a comand on a team
 
Won't say this is better, just easier to understand.
Code:

new max_players = get_maxplayers();
for(new id = 1; id <= max_players; id++)
{
    if(is_user_alive(id) && get_user_team(id) == 1) //1 T, 2 CT
          //slay the noob.
}

Edit: Code updated, somehow forgot what this thread was about, lol. Thanks, hleV.

hleV 03-02-2009 13:23

Re: execute a comand on a team
 
@SnoW, you forgot to check the team.
Code:
new g_MaxPlayers; // Global array which will hold the numer of max players   public plugin_init()         g_MaxPlayers = get_maxplayers(); // Get max players and store it in our array   public YourFunction() {         for (new Client = 1; Client <= g_MaxPlayers; Client++)         {                 if (!is_user_connected(Client))                         continue;                 switch (get_user_team(Client))                 {                         case 1: // Terrorist                         {                                 // Your code (note that ID is Client)                         }                         case 2: // CT                         {                                 // Your code                         }                         case 3: // Spectator                         {                                 // Your code                         }                 }         } }

Bugsy 03-02-2009 21:50

Re: execute a comand on a team
 
Quote:

Originally Posted by SnoW (Post 772430)
Won't say this is better, just easier to understand.
Code:

new max_players = get_maxplayers();
for(new id = 1; id < max_players; id++)
{
    if(is_user_alive(id) && get_user_team(id) == 1) //1 T, 2 CT
          //slay the noob.
}

Edit: Code updated, somehow forgot what this thread was about, lol. Thanks, hleV.

From an 'easier to read' standpoint, sure. That code clearly says loop from 1 to max players, if player alive and is on team X, do this.

However, you are looping through more times than needed (1->maxplayers) and checking get_user_team and is_user_alive with each iteration. My method is more proper and efficient, why not get newer coders started using the correct methods?

Check only specific team and player must be alive:

PHP Code:

new iPlayers[32];
new 
iNum;
//"a" - Don't return dead players 
//"e" - Match with passed team 
get_playersiPlayers iNum ,"ae" ,"CT" );
//get_players( iPlayers , iNum  ,"ae" ,"TERRORIST" )

for( new 0iNum i++)
{
    
//slay iPlayers[i];



fysiks 03-02-2009 22:34

Re: execute a comand on a team
 
@Snow & HleV

I think you both will get runtime errors for checking the team of an empty slot.

(I always get runtime errors for checking the team of empty slots)

Bugsy 03-03-2009 00:10

Re: execute a comand on a team
 
@hlev, this is not an array and may confuse people. I'm sure you are aware and it is just a typo.

PHP Code:

new g_MaxPlayers;  // Global array which will hold the numer of max players
g_MaxPlayers get_maxplayers();  // Get max players and store it in our array 

Quote:

Originally Posted by fysiks (Post 772804)
@Snow & HleV

I think you both will get runtime errors for checking the team of an empty slot.

(I always get runtime errors for checking the team of empty slots)

In that case, an is_user_connected() will also be in order.

SnoW 03-03-2009 01:58

Re: execute a comand on a team
 
Quote:

Originally Posted by Bugsy (Post 772782)
However, you are looping through more times than needed (1->maxplayers) and checking get_user_team and is_user_alive with each iteration. My method is more proper and efficient, why not get newer coders started using the correct methods?

This guy is asking a way how to use a simple native or something like that. I'm just saying that for people who are starting scripting can be little hard to understand the best and optimized ways. I wouldn't start explaining them something todally advanced, if there's a lot of easier way to understand, what would work almost as good as the others. Just thought that if he gots some problems on your way, he could use the other.
Quote:

Originally Posted by fysiks (Post 772804)
@Snow & HleV

I think you both will get runtime errors for checking the team of an empty slot.

(I always get runtime errors for checking the team of empty slots)

Quote:

Originally Posted by Bugsy (Post 772821)
In that case, an is_user_connected() will also be in order.

Somehow you probably didn't see that I first checked if the player is alive, and after that the team. Ofc connected check could be little better here like for example, but he was talking about slaying, so slaying dead players wouldn't be that good.

fysiks 03-03-2009 07:05

Re: execute a comand on a team
 
@Snow: I'm assuming, in an if statement, that all conditions are evaluated before returning a result. With that assumption an empty slot would give 0 for alive and runtime error for team. I've tried asking if this is how it actually does work but I was not given a clear answer.

Also, you would require checking if they are connected if the command does not require being alive in which case you would need to use is_user_connected(). Basically, I was thinking of the general case of "an action on a team" and not just the example he happened to think of.

That's my 3 cents :).

SnoW 03-03-2009 07:17

Re: execute a comand on a team
 
Quote:

Originally Posted by fysiks (Post 772956)
@Snow: I'm assuming, in an if statement, that all conditions are evaluated before returning a result. With that assumption an empty slot would give 0 for alive and runtime error for team. I've tried asking if this is how it actually does work but I was not given a clear answer.

Also, you would require checking if they are connected if the command does not require being alive in which case you would need to use is_user_connected(). Basically, I was thinking of the general case of "an action on a team" and not just the example he happened to think of.

That's my 3 cents :).

For first one, No. If the first one is false(or any other) it won't check the rest of them. I think there's pretty good logic in that, why to check them, if it's known already that the statement is false?
For second, Already said that ye maybe the connect check would be better here cause it's an example, but I just coded it how I could code it in a plugin. Using both checks would be ridicilous and using connect check and slaying after that didn't sound good either.


All times are GMT -4. The time now is 17:10.

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