ID:179234
 
In the reference, it says that break [label] will terminate a loop of that label, but how do you set the label of a loop, or how do you tell what it is?
It seems to me like it works like "goto"...

All you have to do is add a word (usually followed by a colon) to the code right before the loop begins... And then you call goto [the word] when you want to loop to go back to the beginning...

I use goto for a few things in my game... Mostly when a player is required to input some value that needs to be checked for validity and if it doesn't meet the requirements...the loop begins again and they need to input another value...

It works like so:

Value_set()
Anchor: //(or "label" or any word you want to use)
value = input(src, "What is your value?") as num
if (value < 0)
src << "Your value cannot be less than 0."
goto Anchor


It will loop through the proc by going back to the spot where the Anchor was set everytime the player inputs a negative number... The loop will not...well...loop once they give a proper positive number...

I would assume that "break" works in much the same way... Sort of like the following:

Check_First_Key()
Anchor:
for (mob/M in world)
if (M.key)
break Anchor
src << "[M.key] is the first player found."


But that's just speculation... I haven't read much about break yet...nor have I used it... And upon checking the Blue Book...that syntax isn't included... They mention break and continue... But not with labels...

But I think it's a fairly reasonable guess...

Although "break" by itself should work just fine without the label to break most loops... But I suppose there are situations where you might need to specify what loop you want it to break...
Using break with a label is useful if you have nested loops, since you can use it to break out of more than one loop at once. Here's an example:

some_proc()
myLabel:
for(mob/M in world)
for(obj/O in M)
del O
break myLabel

which will find the first obj in the first mob, delete it, then stop execution. If you replace break with continue, it will delete one obj from each mob in the world. This example is pretty trivial, since there are other ways of doing this sort of thing, but it gets the point across.

-AbyssDragon
In response to AbyssDragon
some_proc()
myLabel:
for(mob/M in world)
for(obj/O in M)
del O
break myLabel

Now that I get around to trying it, it doesn't seem to work...

error:myLabel :break failed
In response to Foomer
Foomer wrote:
some_proc()
myLabel:
for(mob/M in world)
for(obj/O in M)
del O
break myLabel

Now that I get around to trying it, it doesn't seem to work...

error:myLabel :break failed

You need to indent under the label

In response to SuperSaiyanGokuX
SuperSaiyanGokuX wrote:
It seems to me like it works like "goto"...

With one major difference:

goto will repeat the loop, while break will skip to the end of the loop.