break and return
#1

Here I`m again..
Now with small problem but will be nice to know.
How could i stop script (return) and use break in one time?
Reason is, i want to stop forwarded script and break a loop.

pawn Код:
for(new h = 0; h < sizeof(HouseInfo); h++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 2.0, HouseInfo[h][hEntrancex], HouseInfo[h][hEntrancey], HouseInfo[h][hEntrancez]))
        {
            if(HouseInfo[h][hOwned] == 1)
            {
                if(HouseInfo[h][hRentabil] == 0)
                {
                    //script
                }
                else
                {
                    //script
                }
            }
            else
            {
                 // script
            }
            break; // here i want to break and return to not allow next loop
        }
    }
    for(new h = 0; h < sizeof(SBizzInfo); h++)
    {
`break; // here i want to break and return something to not allow next loop`
How would i do that in my case?

Tried things like:
pawn Код:
return break;
//---
break;
return 1;
//----
Reply
#2

a method I always like is
new count;

and when you break
count++;

make a if statement, seeing if(count > 1) { Dont run the code } else { Run the code }
Reply
#3

That would be the last what would i use if there are more solutions. (i`m thinking about code optimisation not deoptimisation )
Reply
#4

It has to be placed in an if() or else() statement I believe. Because if the house is owned or not, "break" is never executed. Can you explain a little bit more where/why you want it to break?
Reply
#5

ofcourse.
I want to break loop because it would totally run to 1000 and if player got things in 500 then i would not run loop for nothing if you know what i mean.
pawn Код:
for(new h = 0; h < 1000; h++)
{
     if(h == 500)
     {
         he got things...
         // i dont want to loop another 500 so - break and i dont want to create next loop for nothing - return somehow.
     }
}
for(next loop to about 200)
{ ....
Reply
#6

In that case, you would put break; after "he got things". This would stop the loop there and the code would go on to your next loop. Hope that helps.
Reply
#7

but then i need to stop second loop to and again the same problem..
Reply
#8

break will stop the loop from excecuting which is what you want.
Return stops everything after that from being excecuted.

The example in the first post will work as you want, it is advised to put the return at the end of the callback otherwise everything after the first loop won't be excecuted.

pawn Код:
// Loop 1
// Loop 1 returns true for your if statement
break;
// Loop 2 (Which is a new loop under Loop 1 like in your example)
// Loop 2 ends
return 1;
Hope this helps
Reply
#9

If yould see all my code.
These loops are under OnPlayerPickupPickup.
After loops it checks player position (InRangeOf) but if something was done in loop then i want to break current loop and stop script from continuing.
Ofcourse i know how to do that but that is last thing what would i do, I`m hoping on you...
Reply
#10

I think i get what you mean here is an example of what id do.
pawn Код:
new goal;
for(new h = 0; h < sizeof(HouseInfo); h++)
    {
        if(IsPlayerInRangeOfPoint(playerid, 2.0, HouseInfo[h][hEntrancex], HouseInfo[h][hEntrancey], HouseInfo[h][hEntrancez]))
        {
            if(HouseInfo[h][hOwned] == 1)
            {
                if(HouseInfo[h][hRentabil] == 0)
                {
                   
                    //script
                }
                else
                {
                    //script
                }
            }
            else
            {
                 // script
            }
            goal = 1;//you would need a variable to stop the next loop
            break; // here i want to break and return to not allow next loop
        }
    }
    if(goal == 1)return 1;//if the loop found what it was looking for return 1
    for(new h = 0; h < sizeof(SBizzInfo); h++)
    {
    }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)