AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Loop wont run more than 4 times (https://forums.alliedmods.net/showthread.php?t=154347)

Jelle 04-06-2011 16:16

Loop wont run more than 4 times
 
I have this very weird problem, my loop wont run for more than 4 times. Sometimes it runs just once and other times it runs 4 times. Completely random how it runs.

PHP Code:

public zp_round_started(gamemodeid)
{
    
client_print(0print_chat"[Debug] round started")
    new 
players[32], playerCountiplayer
    get_players
(playersplayerCount"ah")
    
    for ( 
032i++ )
    {
        
player players[i]
        
        
client_print(0print_chat"[Debug] loop is running %d"player)
        
        if ( 
zp_get_user_zombie(player) || zp_get_user_nemesis(player) ) return
        
        
client_print(playerprint_chat"[Debug] You are human")
        
        if ( 
gHasSuperman[player] )
        {
            
client_print(playerprint_chat"[Debug] Giving powers...")
            
sh_set_bonus_hp(playerget_pcvar_num(pcvarHealth))
            
sh_set_bonus_ap(playerget_pcvar_num(pcvarArmor))
            
set_user_gravity(playerget_pcvar_float(pcvarGravity))
        }
    }


As you can see I have tried various debugs in my code, and it never gets any longer than to "loop running 4", so I'm guessing that the loop is done wrong?
I have replaced 32 with "playerCount" to try and force it to run 32 times, but no luck there either.
How can this be happening?

EDIT:
Oh, now it just ran 6 times and apparently I did have one of those id's, and the only thing I got was bonus HP and AP, no gravity for me, nor did I get any debug message.

Exolent[jNr] 04-06-2011 16:27

Re: Loop wont run more than 4 times
 
return stops the whole function from executing anything after it.
Therefore, if a player is a zombie or nemesis, all other players after it won't be checked.
You should use continue to go to the next loop iteration.

Jelle 04-06-2011 16:32

Re: Loop wont run more than 4 times
 
So:

PHP Code:

if ( zp_get_user_zombie(player) || zp_get_user_nemesis(player) ) continue 

Not sure how continue works as I never worked with it. But wouldn't that just make the plugin continue and check if gHasSuperman, even if the guy is a zombie, or is the continue only for the loop?

Exolent[jNr] 04-06-2011 16:39

Re: Loop wont run more than 4 times
 
continue stops executing the loop for that 1 iteration and goes to the next one.
Also, continue can be used in all types of loops.

Example:

Code:
for(new i = 0; i < 4; i++) {     if(i == 1) continue;         server_print("%d", i); }

This would be the process:

Code:

create and initialize i to 0
0 < 4 so do loop
0 is not 1, so go past this
print 0
0++ = 1
1 < 4 so do loop
1 is 1, so do not go past this
1++ = 2
2 < 4 so do loop
2 is not 1, so go past this
print 2
2++ = 3
3 < 4 so do loop
3 is not 1, so go past this
print 3
3++ = 4
4 is not < 4 so exit loop

Print output:

Code:

0
2
3


Jelle 04-06-2011 16:43

Re: Loop wont run more than 4 times
 
Oh, thank you for that!
I have tested it and it works as I want it to, awesome.

Elusive138 04-06-2011 21:35

Re: Loop wont run more than 4 times
 
Also, client_print(0, print_chat, "[Debug] loop is running %d", player) would only print the player id that the current iteration is using. Use client_print(0, print_chat, "[Debug] loop is running %d", i)to print the actual loop iteration number.

lis_16 04-07-2011 05:10

Re: Loop wont run more than 4 times
 
Also for sure add:

PHP Code:

if(!is_user_connected(i)) return PLUGIN_CONTINUE 

Your server can crash without suitable if with error invalid player index xx or something like that.

xPaw 04-07-2011 09:24

Re: Loop wont run more than 4 times
 
Quote:

Originally Posted by lis_16 (Post 1445670)
Also for sure add:

PHP Code:

if(!is_user_connected(i)) return PLUGIN_CONTINUE 

Your server can crash without suitable if with error invalid player index xx or something like that.

Don't suggest bullshit, he uses get_players()

lis_16 04-07-2011 10:01

Re: Loop wont run more than 4 times
 
But loop is for 1 to 32.

Exolent[jNr] 04-07-2011 18:03

Re: Loop wont run more than 4 times
 
Quote:

Originally Posted by lis_16 (Post 1445737)
But loop is for 1 to 32.

I'm sure he changed it from pnum to 32 just to see if that was what was causing his errors.


All times are GMT -4. The time now is 19:57.

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