Loop problem.
#9

Nice to Estonians around here.

Anyways, why not have just one array to store which team the player is in.
pawn Код:
new pTeam[MAX_PLAYERS];
#define TIIM_ROHELINE 1
#define TIIM_PUNANE 2

// Set it whenever you assign a team like PunaneTiim[playerid] = 1 before, but do pTeam[playerid] = TIIM_PUNANE instead!

// And reset in OnPlayerDisconnect!
public OnPlayerDisconnect(playerid)
{
    pTeam[playerid] = 0;
}
This logic allows you to improve your loop, so far you don't even actually need a IsPlayerConnected check (if a player is disconnected, pTeam value is 0).
I don't see an obvious error, but might be because I'm tired or cannot just spot anything that would cause such behavior. However what you could ultimately try is to determine which team wins before entering a loop at all, this way you don't have to run 3 if-statements in your loop every iteration.

Also a small logic approach here:
pawn Код:
redScore = 2;
greenScore = 2;
if(redScore > greenScore)
    // red team won
else if(greenScore > redScore)
    // green team won
else
    // scores are equal
is exactly the same as
pawn Код:
redScore = 2;
greenScore = 2;
if(redScore > greenScore)
    // red team won
else if(greenScore > redScore)
    // green team won
else if(greenScore == redScore)
    // scores are equal
I don't know if this is somehow optimized by the compiler or if it makes a difference, but it is a better practice to write the code like my first example anyways, without the unneeded extra if-statement.

Also, if you ask about the break statement, a code like this:
pawn Код:
for(new i = 0; i != 10; i++)
{
    if(i == 5)
    {
        break;
    }
    printf("%d", i);
}
Would output:
0 1 2 3 4
... and then stop processing the loop. The same goes for players. If at, lets say iterator 2 (player 2) you decide to use break, your loop only covers 2 players and then stops. So nope, you most likely don't need to use break.

However an useful way to use continue, another interesting keyword, is to do something like this:
pawn Код:
// before:
for(new i = 0; i != MAX_PLAYERS; i++)
{
    if(pTeam[i] != 0)
    {
        // code
    }
}

// after
for(new i = 0; i != MAX_PLAYERS; i++)
{
    if(pTeam[i] == 0)
        continue;
    // code
}
These codes produce the same result.
Reply


Messages In This Thread
Loop problem. - by Dan. - 12.05.2012, 11:39
Re: Loop problem. - by AndreT - 12.05.2012, 11:58
Re: Loop problem. - by Dan. - 12.05.2012, 13:00
Re: Loop problem. - by RedWingz - 12.05.2012, 13:05
Re: Loop problem. - by Dan. - 12.05.2012, 13:08
Re: Loop problem. - by RedWingz - 12.05.2012, 13:10
Re: Loop problem. - by Dan. - 12.05.2012, 13:16
Re: Loop problem. - by Dan. - 12.05.2012, 18:48
Re: Loop problem. - by AndreT - 12.05.2012, 20:29

Forum Jump:


Users browsing this thread: 3 Guest(s)