Raised This Month: $ Target: $400
 0% 

Loop wont run more than 4 times


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Jelle
[b]MOAR CANDY[/b]
Join Date: Aug 2009
Location: Denmark
Old 04-06-2011 , 16:16   Loop wont run more than 4 times
Reply With Quote #1

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.
__________________
No idea what to write here...

Last edited by Jelle; 04-06-2011 at 16:20.
Jelle is offline
Send a message via MSN to Jelle
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-06-2011 , 16:27   Re: Loop wont run more than 4 times
Reply With Quote #2

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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Jelle
[b]MOAR CANDY[/b]
Join Date: Aug 2009
Location: Denmark
Old 04-06-2011 , 16:32   Re: Loop wont run more than 4 times
Reply With Quote #3

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?
__________________
No idea what to write here...
Jelle is offline
Send a message via MSN to Jelle
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-06-2011 , 16:39   Re: Loop wont run more than 4 times
Reply With Quote #4

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
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Jelle
[b]MOAR CANDY[/b]
Join Date: Aug 2009
Location: Denmark
Old 04-06-2011 , 16:43   Re: Loop wont run more than 4 times
Reply With Quote #5

Oh, thank you for that!
I have tested it and it works as I want it to, awesome.
__________________
No idea what to write here...
Jelle is offline
Send a message via MSN to Jelle
Elusive138
Senior Member
Join Date: Dec 2010
Old 04-06-2011 , 21:35   Re: Loop wont run more than 4 times
Reply With Quote #6

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.
Elusive138 is offline
lis_16
Senior Member
Join Date: Feb 2008
Old 04-07-2011 , 05:10   Re: Loop wont run more than 4 times
Reply With Quote #7

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.
lis_16 is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 04-07-2011 , 09:24   Re: Loop wont run more than 4 times
Reply With Quote #8

Quote:
Originally Posted by lis_16 View Post
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()
__________________
xPaw is offline
lis_16
Senior Member
Join Date: Feb 2008
Old 04-07-2011 , 10:01   Re: Loop wont run more than 4 times
Reply With Quote #9

But loop is for 1 to 32.
lis_16 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 04-07-2011 , 18:03   Re: Loop wont run more than 4 times
Reply With Quote #10

Quote:
Originally Posted by lis_16 View Post
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.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] 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 19:57.


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