SA-MP Forums Archive
How to stop a loop? - 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: How to stop a loop? (/showthread.php?tid=411092)



How to stop a loop? - stormchaser206 - 27.01.2013

Hello,
I need to know how to stop a loop. I am getting errors in my script because break; is "out of context".
So, I need to know a way other than break;

Thanks.


Re: How to stop a loop? - Memoryz - 27.01.2013

for a while loop you can do

pawn Код:
new bool:looping = true;

while(looping)
{
   // bla bla bla
   looping = false; // when u want to stop looping
}



Re: How to stop a loop? - u3ber - 27.01.2013

Quote:
Originally Posted by stormchaser206
Посмотреть сообщение
I need to know a way other than break;
you could use return to pass control back to some location or just return some value.


Re: How to stop a loop? - FUNExtreme - 27.01.2013

You can use 'break;' to stop a loop. It should work without a problem so I'm not sure why you are experiencing this


Re: How to stop a loop? - MP2 - 27.01.2013

Show your loop.


Re: How to stop a loop? - stormchaser206 - 27.01.2013

pawn Код:
switch(PlayerInfo[playerid][Xp])
    {
        case 20:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Private II!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }

        case 45:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Private First Class!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 70:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Specialist!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 120:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Corporal!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 170:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 250:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant First Class!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 400:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Master Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 650:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to First Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 1000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 1500:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Command Sergeant Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 2100:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant Major of the Army!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 2800:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Warrant Officer!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 3900:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 2!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 5000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 3!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 7500:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 4!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 10000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 5!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 15000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Second Lieutenant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 19000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to First Lieutenant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 27000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Captain!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 35000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 50000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Lieutenant Colonel!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 70000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Colonel!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 100000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Brigadier General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 120000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Major General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 150000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Lieutenant General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 200000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
       
        case 300000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to General of the Army!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
            break;
        }
    }



Re: How to stop a loop? - u3ber - 27.01.2013

PAWN's switch statements aren't fall-through, (just remove it) oh and btw, that isn't a loop...


Re: How to stop a loop? - stormchaser206 - 27.01.2013

it isn't a loop?

EDIT: Then I mean how do you set it to not go until its equal to the next one?


Re: How to stop a loop? - u3ber - 27.01.2013

Quote:
Originally Posted by stormchaser206
Посмотреть сообщение
it isn't a loop?
nope. a loop is just repeated code (in cavemen-like explanation). You code is equivalent to the if-else variant.

Quote:
Originally Posted by OP
EDIT: Then I mean how do you set it to not go until its equal to the next one?
what exactly are you trying to do?


Re: How to stop a loop? - SchurmanCQC - 28.01.2013

Quote:
Originally Posted by stormchaser206
Посмотреть сообщение
pawn Код:
//code here
Switch statements aren't loops. They're conditional like if, but they're more dynamic.

Putting break in a switch statement is required for languages like C# and C++, but not needed in Pawn. (So I can understand why you're doing this.)

EDIT: You can probably fix the spamming by putting a default statement in that.

I've improved your code:

pawn Код:
switch(PlayerInfo[playerid][Xp])
    {
        case 20:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Private II!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 45:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Private First Class!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 70:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Specialist!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 120:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Corporal!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 170:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 250:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant First Class!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 400:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Master Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 650:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to First Sergeant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 1000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 1500:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Command Sergeant Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }      
        case 2100:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Sergeant Major of the Army!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 2800:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Warrant Officer!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 3900:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 2!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 5000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 3!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 7500:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 4!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 10000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Chief Warrant Officer 5!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 15000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Second Lieutenant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 19000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to First Lieutenant!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 27000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Captain!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 35000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Major!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
       
        case 50000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Lieutenant Colonel!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 70000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Colonel!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 100000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Brigadier General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 120000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Major General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 150000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to Lieutenant General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 200000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to General!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        case 300000:
        {
            SendClientMessage(playerid, COLOR_GREEN, "You have been promoted to General of the Army!");
            PlayAudioStreamForPlayer(playerid, "https://dl.dropbox.com/u/77376451/rankupmwffa.mp3");
        }
        default: return 1;
    }