SA-MP Forums Archive
Loops only run one function? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Loops only run one function? (/showthread.php?tid=510157)



Loops only run one function? - AphexCCFC - 30.04.2014

pawn Код:
stock CheckHouse(playerid)
{
    if(PlayerInfo[playerid][pInHouse] >= 0)
    {
        for(new idx = 0; idx < MAX_HOUSES; idx++)
        {
            if(!strcmp(PlayerInfo[playerid][pInAddress], HouseInfo[idx][hAddress], false))
            {
                SetSpawnInfo(playerid, 0, PlayerInfo[playerid][pSkin], HouseInfo[idx][hExitPos][0], HouseInfo[idx][hExitPos][1], HouseInfo[idx][hExitPos][2], HouseInfo[idx][hExitPos][3], 0, 0, 0, 0, 0, 0);
                SetPlayerPos(playerid, HouseInfo[idx][hExitPos][0], HouseInfo[idx][hExitPos][1], HouseInfo[idx][hExitPos][2]);
                SetPlayerFacingAngle(playerid, HouseInfo[idx][hExitPos][3]);
                SetInterior(playerid, HouseInfo[idx][hInterior]);
                SetVirtualWorld(playerid, HouseInfo[idx][hVirtualWorld]);
                PlayerInfo[playerid][pInHouse] = HouseInfo[idx][hHouseID];
                format(PlayerInfo[playerid][pInAddress], 32, "%s", HouseInfo[idx][hAddress]);
                break;
            }
            if(strcmp(PlayerInfo[playerid][pInAddress], HouseInfo[idx][hAddress], false))
            {
                SetSpawnInfo(playerid, 0, PlayerInfo[playerid][pSkin], -2656.6123, 635.8759, 14.4531, 182.7413, 0, 0, 0, 0, 0, 0);
                SetPlayerPos(playerid, -2656.6123, 635.8759, 14.4531);
                SetPlayerFacingAngle(playerid, 182.7413);
                SetInterior(playerid, 0);
                SetVirtualWorld(playerid, 0);
                PlayerInfo[playerid][pInHouse] = -1;
                format(PlayerInfo[playerid][pInAddress], 32, "None");
                SendClientMessage(playerid, COLOR_ORANGE, "The house you were in has been edited or deleted. You have been set back to the newb spawn.");
                break;
            }
        }
    }
    else
    {
        SetSpawnInfo(playerid, 0, PlayerInfo[playerid][pSkin], PlayerInfo[playerid][pPos][0], PlayerInfo[playerid][pPos][1], PlayerInfo[playerid][pPos][2], PlayerInfo[playerid][pPos][3], 0, 0, 0, 0, 0, 0);
        SetPlayerPos(playerid, PlayerInfo[playerid][pPos][0], PlayerInfo[playerid][pPos][1], PlayerInfo[playerid][pPos][2]);
        SetPlayerFacingAngle(playerid, PlayerInfo[playerid][pPos][3]);
        SetInterior(playerid, PlayerInfo[playerid][pInterior]);
        SetVirtualWorld(playerid, PlayerInfo[playerid][pVirtualWorld]);
        format(PlayerInfo[playerid][pInAddress], 32, "None");
    }
    return 1;
}
Very close to completing my housing system, last problem I have..
If I remove the second "if", the first one works, if I keep it, only the second one works.
Can I not use two different "if" functions inside a loop?


Re: Loops only run one function? - Dignity - 30.04.2014

I'm very tired so please bare with me.

If I understand correctly, I think "break;" causes your loop to stop completely. Either removing "break;" or changing the second "if" to "else if" may fix it.

If you want to break the loop regardless, I suggest placing the break; statement outside any "if" statement. (i.e. before the closing callback of the loop).

Sorry if I don't make alot of sense, would appreciate someone else either rephrasing my words or correcting them.


Re: Loops only run one function? - SkittlesAreFalling - 30.04.2014

Use else, not that that matters, and remove the breaks.


Re: Loops only run one function? - AphexCCFC - 30.04.2014

Thanks guys, managed to find a work around. Removing break and adding else didn't work. Would have been better if it worked this way though