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

Stuck in a loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Player 1
Member
Join Date: Apr 2012
Old 05-29-2012 , 13:23   Stuck in a loop
Reply With Quote #1

When this code runs the server immediately crashes. I'm think the while loop isn't terminating properly, but I can't see why it wouldn't.

My array may also be the problem since I have no experience working with this function.

Just to be clear, I'm working on a balancer that evens the teams based on the player's rank. I'm trying to store all the ranks in an array using the client's index as the array index.
'count' is the level I'm searching for in the array. If it's not found, it should decrement and search for the next number.

Can anyone see what might be going wrong?
PHP Code:
BalanceTeams() {
    new 
Handle:Array = CreateArray(433);
    new 
bool:teambool:complete;
    new 
count 80;
    
    for (new 
1<= MaxClientsi++) {
        if (
IsClientInGame(i)) {
            
SetArrayCell(Array, iGetEntData(g_iPlayerManagerg_iRankOffset 4*i));
        }
    }

    while(!
complete) {
        new 
client FindValueInArray(Array, count);
        if (
client > -1) {
            if (
IsClientInGame(client)) {
                
ChangeClientTeam(client1);
                
ChangeClientTeam(client,team 3);
                
team = !team;
                
SetArrayCell(Array, client, -1);
            }
            continue;
        }
        else {
            
count--;
            if (
count 0) {
                break;
            }
            continue;
        }
    }
    return;

Player 1 is offline
11530
Veteran Member
Join Date: Sep 2011
Location: Underworld
Old 05-29-2012 , 13:38   Re: Stuck in a loop
Reply With Quote #2

I haven't used SetArrayCell(Array, i, GetEntData(g_iPlayerManager, g_iRankOffset + 4*i)); before, but perhaps thats the offending code since the rest looks ok.
__________________

Last edited by 11530; 05-29-2012 at 13:42.
11530 is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 05-29-2012 , 14:32   Re: Stuck in a loop
Reply With Quote #3

uhm, complete is always false ? you are not setting it to true at any time.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Player 1
Member
Join Date: Apr 2012
Old 05-29-2012 , 14:40   Re: Stuck in a loop
Reply With Quote #4

But I'm using break. I had complete be set to true at one point, then changed to break to see if it made a difference.

@11530
It is just getting an integer value and setting it in the array. Unless I'm misunderstanding exactly what a cell is.

Last edited by Player 1; 05-29-2012 at 14:41.
Player 1 is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 05-29-2012 , 14:59   Re: Stuck in a loop
Reply With Quote #5

Quote:
Originally Posted by Player 1 View Post
But I'm using break. I had complete be set to true at one point, then changed to break to see if it made a difference.

@11530
It is just getting an integer value and setting it in the array. Unless I'm misunderstanding exactly what a cell is.
make complete = true; before the break.
Mitchell is offline
Player 1
Member
Join Date: Apr 2012
Old 05-29-2012 , 15:41   Re: Stuck in a loop
Reply With Quote #6

Quote:
Originally Posted by Mitchell View Post
make complete = true; before the break.
PHP Code:
        else {
            
count--;
            if (
count 0) {
                
complete true;
                break;
            } 
Didn't change anything
Player 1 is offline
Leonardo
Veteran Member
Join Date: Feb 2010
Location: 90's
Old 05-29-2012 , 16:02   Re: Stuck in a loop
Reply With Quote #7

Code:
while( count >= 0 )
{
}
Leonardo is offline
blodia
Veteran Member
Join Date: Sep 2009
Location: UK
Old 05-29-2012 , 16:06   Re: Stuck in a loop
Reply With Quote #8

PHP Code:
BalanceTeams() {
    new 
Handle:Array = CreateArray(433);
    new 
bool:team;
    new 
count 80;
    
    for (new 
1<= MaxClientsi++) {
        if (
IsClientInGame(i)) {
            
SetArrayCell(Array, iGetEntData(g_iPlayerManagerg_iRankOffset 4*i));
        }
    }

    while(
count 0) {
        new 
client FindValueInArray(Array, count);
        if (
client > -1) {
            if (
IsClientInGame(client)) {
                
ChangeClientTeam(client1);
                
ChangeClientTeam(client,team 3);
                
team = !team;
                
SetArrayCell(Array, client, -1);
            }
        }
        else {
            
count--;
        }
    }
    return;

try that, you break when a certain condition is met so why not just use that for the loop condition to start with, also you don't need any of the continues as you placed them just before the loop would start again anyway.
blodia is offline
Player 1
Member
Join Date: Apr 2012
Old 05-29-2012 , 16:10   Re: Stuck in a loop
Reply With Quote #9

Quote:
Originally Posted by Leonardo View Post
Code:
while( count >= 0 )
{
}
Not that either.

I'm almost certain it's not the loop at this point. It has to be the array. Trouble is, I don't see anything wrong with that either.
Player 1 is offline
Player 1
Member
Join Date: Apr 2012
Old 05-29-2012 , 16:17   Re: Stuck in a loop
Reply With Quote #10

That did it blodia. Thanks for your help everyone!
Player 1 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 12:50.


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