Foreach
#1

When I try to apply foreach, commands where you need to enter a players name say that there is no such player, what did I do wrong?

Original:

Код:
stock GetPlayeridMid(name[])
{
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        if (IsPlayerConnected(i))
        {
            new gPlayerName[MAX_PLAYER_NAME];
            GetPlayerName(i, gPlayerName, MAX_PLAYER_NAME);
            if(strfind(gPlayerName, name, true) != -1)
            {
                return i;
            }
        }
    }
return INVALID_PLAYER_ID;
}
With foreach:

Код:
stock GetPlayeridMid(name[])
{
        foreach(Player, i)
        {
            new gPlayerName[MAX_PLAYER_NAME];
            GetPlayerName(i, gPlayerName, MAX_PLAYER_NAME);
            if(strfind(gPlayerName, name, true) != -1)
            {
                return i;
            }
        }
return INVALID_PLAYER_ID;
}
And can I replace something like this with foreach, if yes, how?

Код:
new AutoSavet[MAX_PLAYERS];
Reply
#2

Try using this:
pawn Код:
stock GetPlayeridMid(name[])
{
        foreach(Player, i)
        {
            new gPlayerName[MAX_PLAYER_NAME];
            GetPlayerName(i, gPlayerName, MAX_PLAYER_NAME);
            if(strfind(gPlayerName, name, true) != -1)
            {
                return i;
                break;
            }
        }
    return INVALID_PLAYER_ID;
}
Reply
#3

I get unreachable code error

Tried putting break; a bit lower, the error was gone, but it still says no such player.

PHP код:
stock GetPlayeridMid(name[])
{
        foreach(
Playeri)
        {
            new 
gPlayerName[MAX_PLAYER_NAME];
            
GetPlayerName(igPlayerNameMAX_PLAYER_NAME);
            if(
strfind(gPlayerNamenametrue) != -1)
            {
                return 
i;
            }
         break;
        }
    return 
INVALID_PLAYER_ID;

Reply
#4

Try putting the break; ABOVE the return i; If that doesn't work, then I'm not sure what will :S
Reply
#5

Quote:
Originally Posted by BenzoAMG
Посмотреть сообщение
Try putting the break; ABOVE the return i; If that doesn't work, then I'm not sure what will :S
Nope :S
Reply
#6

Just delete the break, it stop the action of foreach.
Reply
#7

Quote:
Originally Posted by Vukilore
Посмотреть сообщение
Just delete the break, it stop the action of foreach.
The original change had no break; ,but as I said in the first post, it says that there is no such player.
Reply
#8

Oh how stupid of me :S I was testing the commands on NPCs and foreach only loops through players The original change works fine.

Though another question:

Is this replaceable with foreach, if yes, how:

pawn Код:
new AutoSavet[MAX_PLAYERS];
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)