Problem with a loop
#1

I dont understand this, as i create the gang, it creates 30 gangs at the same time:

pawn Код:
CMD:gcreate(playerid,params[])
{
    if(playerGang[playerid]>0)
        return SendClientMessage(playerid, -1, ""COL_RED"ERROR:{FFFFFF} You are already in a gang.");
    if(sscanf(params,"s[16]",params[0]))
        return SendClientMessage(playerid, -1, ""COL_LIGHTBLUE"Usage:{FFFFFF} /gcreate <gangname> (Max 16 char)");
    new string[128];
    for(new i = 1; i < MAX_GANGS; i++)
    {
        if(gangInfo[i][0]==0)
        {
            //name gang
            format(gangNames[i], MAX_GANG_NAME, "%s", params[0]);
            //Gang exists
            gangInfo[i][0]=1;
            //There is one member
            gangInfo[i][1]=1;
            //Gang color is player's color
            gangInfo[i][2]=PlayerColors[playerid];
            //Player is the first gang member
            gangMembers[i][0] = playerid;
            format(string, sizeof(string),"You have created the gang '%s' (id: %d)", gangNames[i], i);
            SendClientMessage(playerid, COLOR_GREEN, string);
            playerGang[playerid]=i;
        }
    }
    return 1;
}
I know it has something to do with the loop, but why is it working
perfectly at the old landgrab gamemodes? Its the same code.
Anyone could take a look at it and tell me whats wrong?
Thanks, regards.
Reply
#2

pawn Код:
CMD:gcreate(playerid,params[])
{
    if(playerGang[playerid]>0)
        return SendClientMessage(playerid, -1, ""COL_RED"ERROR:{FFFFFF} You are already in a gang.");
    if(!strlen(params[0]))
        return SendClientMessage(playerid, -1, ""COL_LIGHTBLUE"Usage:{FFFFFF} /gcreate <gangname> (Max 16 char)");
    new string[128];
    for(new i = 1; i < MAX_GANGS; i++)
    {
        if(gangInfo[i][0]==0)
        {
            //name gang
            format(gangNames[i], MAX_GANG_NAME, "%s", params[0]);
            //Gang exists
            gangInfo[i][0]=1;
            //There is one member
            gangInfo[i][1]=1;
            //Gang color is player's color
            gangInfo[i][2]=PlayerColors[playerid];
            //Player is the first gang member
            gangMembers[i][0] = playerid;
            format(string, sizeof(string),"You have created the gang '%s' (id: %d)", gangNames[i], i);
            SendClientMessage(playerid, COLOR_GREEN, string);
            playerGang[playerid]=i;
            return 1;
        }
    }
    return 1;
}
We return a value in the loop when an un-occupied gang slot is found, so that the loop does not continue running trying to find an empty slot (Can also use break). I do believe that is what you're looking for. Additionally as you can see I removed the use of sscanf, as there's no need to unformat a string that doesn't need to be unformatted, all you want to know is if the player entered a value for it or not. Either way you should also check the length of the entered parameter, to make sure the player didn't go over your 16 character limit.
Reply
#3

I dont think so that the problem would be sscanf since it works perfect and returns
the exact command usage without warnings in the logs, therefore, ******'s foreach will fix the problem?

EDIT: JaTochNietDan, Ok il see if the bugs gone.
Reply
#4

Quote:
Originally Posted by OldDirtyBastard
Посмотреть сообщение
I dont think so that the problem would be sscanf since it works perfect and returns
the exact command usage without warnings in the logs, therefore, ******'s foreach will fix the problem?
Have you read my post? I explain your problem there.

(Just making sure, some people skip through the code without reading the text underneath that explains everything)
Reply
#5

I used the 'break;' and now it works fine.
So a loop should always return a value or have a 'break' so it wont repeat all over again?
Reply
#6

Alright, thanks. I'll remember that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)