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

looping through players...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 08-20-2010 , 22:11   looping through players...
Reply With Quote #1

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?
__________________
+|- KARMA Respectively

HLM is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-20-2010 , 22:36   Re: looping through players...
Reply With Quote #2

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++)
{
    // ...
}
__________________
fysiks is offline
HLM
Senior Member
Join Date: Apr 2008
Location: C:\WINDOWS\System32
Old 08-20-2010 , 23:39   Re: looping through players...
Reply With Quote #3

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 */ 
        

__________________
+|- KARMA Respectively

HLM is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-21-2010 , 04:31   Re: looping through players...
Reply With Quote #4

new x ; declare the variable before the loop or with i : for ( new i = 0, x; ...
__________________
Arkshine is offline
Mini_Midget
Veteran Member
Join Date: Jan 2006
Location: It's a mystery.
Old 08-23-2010 , 04:40   Re: looping through players...
Reply With Quote #5

Quote:
Originally Posted by Arkshine View Post
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.
__________________
It's a mystery.
Mini_Midget is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 08-23-2010 , 05:00   Re: looping through players...
Reply With Quote #6

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.
__________________
Arkshine is offline
GXLZPGX
Veteran Member
Join Date: Sep 2009
Old 08-23-2010 , 05:15   Re: looping through players...
Reply With Quote #7

Quote:
Originally Posted by Mini_Midget View Post
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.
__________________
Currently accepting payment US DOLLARS ONLY for custom plugins, contact me through PM.
GXLZPGX is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 08-23-2010 , 05:46   Re: looping through players...
Reply With Quote #8

Use "d" flags in get_players so you collect only bots.

PHP Code:
get_players(iPlayersiNum"d"
__________________
- tired and retired -

- my plugins -
ConnorMcLeod 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 15:16.


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