AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   looping through players... (https://forums.alliedmods.net/showthread.php?t=135917)

HLM 08-20-2010 22:11

looping through players...
 
PHP Code:

public client_connect(id)
{
    
server_print("someone connected")
    
server_print("%d players with a bot limit of %d with a server capacity of %d",get_playersnum(1), get_cvar_num("fb_maxbots"), get_maxplayers())
    if(
get_playersnum(1) > get_cvar_num("fb_maxbots"))
    {
        
server_print("loop begin")
        new 
iPlayers[32],iNum get_maxplayers()
        for(new 
i=1;i<iNum;i++) 
        {
            new 
x=iPlayers[i]
            
server_print("iNum = %d",iNum)
            
server_print("Attempting to kick %d"get_user_userid(x))
            
server_cmd("kick #%d"xget_user_userid(x))
            
server_print("[SKYNET] Kicked #%d due to slot overflow",  get_user_userid(x))
            
server_print("execcing...")
            
server_exec()
            
server_print("execced!")
            
server_print("Breaking...")
            if(
get_playersnum(1) <= get_cvar_num("fb_maxbots"))
                break;
            else
                
server_print("ERROR! relooping (AFAIK)")
            

            
/* Do something */ 
        
}
        
//server_cmd("bot ^"kickall^"")
        //server_cmd("kick #%d",get_user_userid(g_lastbot))
        
    
}


the problem is, it keeps looping and doing 0, I set it i to 1, so then obviously iPlayers[i] = 0, how do I go about resolving this?

fysiks 08-20-2010 22:36

Re: looping through players...
 
When you declare iPlayers[32] all 32 cells contain the value 0.

Use:

Code:

new iPlayers[32], iNum
get_players(iPlayers, iNum)
for(new i = 0; i < iNum; i++)
{
    // ...
}


HLM 08-20-2010 23:39

Re: looping through players...
 
That worked, thanks!


Final Outcome:
PHP Code:

new iPlayers[32], iNum
        get_players
(iPlayersiNum)
        for(new 
0iNumi++) 
        {
            new 
x=iPlayers[i]
            if(
is_user_bot(x))
            {
                
server_print("iNum = %d",iNum)
                
server_print("Attempting to kick %d"get_user_userid(x))
                
server_cmd("kick #%d"get_user_userid(x))
                
server_print("[SKYNET] Kicked #%d due to slot overflow",  get_user_userid(x))
                
server_print("execcing...")
                
server_exec()
                
server_print("execced!")
                
server_print("Breaking...")
                if(
get_playersnum(1) <= get_cvar_num("fb_maxbots"))
                    break;
                else
                    
server_print("ERROR! relooping (AFAIK)")
            }
            

            
/* Do something */ 
        



Arkshine 08-21-2010 04:31

Re: looping through players...
 
new x ; declare the variable before the loop or with i : for ( new i = 0, x; ...

Mini_Midget 08-23-2010 04:40

Re: looping through players...
 
Quote:

Originally Posted by Arkshine (Post 1277560)
new x ; declare the variable before the loop or with i : for ( new i = 0, x; ...

Hmm... Can you explain why? I always tend to declare the new "id' variables inside the loop.

Arkshine 08-23-2010 05:00

Re: looping through players...
 
You ask why ? Each var is zeroed. It's quite obvious to create one time the var before the loop instead of x times is more efficient. If you can, don't create var in a loop.

GXLZPGX 08-23-2010 05:15

Re: looping through players...
 
Quote:

Originally Posted by Mini_Midget (Post 1279499)
Hmm... Can you explain why? I always tend to declare the new "id' variables inside the loop.

Since you may do it a lot depending on how many people are usually in your server, it's more efficient that everytime you loop, you're not creating new variables constantly. It's better that the variable is already created, so the plugin can constantly use that variable.

ConnorMcLeod 08-23-2010 05:46

Re: looping through players...
 
Use "d" flags in get_players so you collect only bots.

PHP Code:

get_players(iPlayersiNum"d"



All times are GMT -4. The time now is 22:03.

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