[Help] Loops
#1

I've a problem with loops.
I'm trying to make a command which will check if the player is near a house (the loop is going through all houses)
and the problem is that it counts just house id 1 (if I add a return/break).
and if I don't add a return/break it spams my chat:

thats the loop:
pawn Код:
for(new h = 1; h < sizeof(HouseInfo); h++)
Reply
#2

Show your full loop, including return/break and specify what happens exactly. return will finish the whole loop, continue will just skip one run of it.
Reply
#3

pawn Код:
if (strcmp(cmd, "/collect", true) == 0)
{
    if(PlayerInfo[playerid][pJob] == 1)
    {
        for(new h = 1; h < MAX_HOUSES; h++)
        {
            if(PlayerToPoint(5, playerid, HouseInfo[h][hEntrancex], HouseInfo[h][hEntrancey], HouseInfo[h][hEntrancez]))
            {
                if(HouseInfo[h][hTrash] < 1) return SCM(playerid, COLOR_LIGHTRED, "This house has no trash.");
                gHasTrash[playerid] = HouseInfo[h][hTrash];
                format(string, sizeof(string), "You have collected %d trash, now put it in the trash master!", HouseInfo[h][hTrash]);
                SCM(playerid, COLOR_GREEN, string);
                HouseInfo[h][hTrash] = 0;
            }
            else
            {
                SCM(playerid, COLOR_LIGHTRED, "You are not near any house.");
            }
        }
    }
    else
    {
        SCM(playerid, COLOR_GREY, "* You are not a garbage collector.");
    }
    return 1;
}
The problem is:
if I'm not near any house, it spams my chat, if I'm on a house which is not id 1, it says "You are not near any house"
Reply
#4

Try it, untested.
pawn Код:
if (strcmp(cmd, "/collect", true) == 0)
{
    if(PlayerInfo[playerid][pJob] == 1)
    {
        new results=0;
       
        for(new h = 1; h < MAX_HOUSES; h++)
        {
            if(PlayerToPoint(5, playerid, HouseInfo[h][hEntrancex], HouseInfo[h][hEntrancey], HouseInfo[h][hEntrancez]))
            {
                results++;
                if(HouseInfo[h][hTrash] < 1)
                {
                    SCM(playerid, COLOR_LIGHTRED, "This house has no trash.");
                    continue;
                }
                gHasTrash[playerid] = HouseInfo[h][hTrash];
                format(string, sizeof(string), "You have collected %d trash, now put it in the trash master!", HouseInfo[h][hTrash]);
                SCM(playerid, COLOR_GREEN, string);
                HouseInfo[h][hTrash] = 0;
            }        
        }
                if(results==0)
                SCM(playerid,COLOR_GREEN,"You are not near any house.");
    }
    else
    {
        SCM(playerid, COLOR_GREY, "* You are not a garbage collector.");
    }
    return 1;
}
Reply
#5

That worked, thanks
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)