8

How do you make a breakable loop in Scratch? I'm using Scratch 2.0 and can't find any good way to make a loop breakable, from inside of the loop itself.

Nebula
  • 6,614
  • 4
  • 20
  • 40

6 Answers6

13

Disclaimer:

There is no perfect way to do it. If you can possibly stand this true fact then feel free to continue.


There are a few different ways you could do it.

With repeat until

The first and most simple one follows this:

scratchblocks

But this isn't technically part of the script - it's just repeating until some value returns true.

With a custom block (stop this script)

In order to do it inside of the script, you'll need to use a sneaky little trick with custom blocks.

Create a custom block called whatever you want - but probably along the lines of "breakable loop". Inside of it, create this script:

scratchblocks

By using stop script we are breaking out of the script that is currently running - which, according to Scratch, is the custom block.

See the result! (as scratchblocks)

With broadcast and wait

You could also use a broadcast-and-wait method, very similar to above:

scratchblocks

Though I highly suggest you don't use this method, as if any other sprites have breakable loops you'll need to rename each one, which can be tedious after using a lot of loops in a lot of sprites!


(Note this bug has been fixed in version 442 of the editor and such the following no longer applies.)

Help! My project is lagging a bunch now!

As @foi has noticed, if your code must be run inside of a frame you probably checked run without screen refresh. Unfortunately, due to a bug in the Scratch player, this causes the program to essentially break after the stop this script block has been activated. How can you handle this?

It follows the same principle you use when you use a run without screen refresh custom block inside of a forever loop - the loop doesn't use screen refresh while the inside does, allowing for instant animations whether or not one is using turbo mode.

Here's an example - the image is really too long to be embedded, so see it here instead.

Community
  • 1
  • 1
Nebula
  • 6,614
  • 4
  • 20
  • 40
3

You can make a variable inside or outside of the repeat and make your script like this:

repeat until [[my variable] = [e.g: 1]]
your code
your code
your code
your code
end of repeat until
Panchi
  • 295
  • 1
  • 3
  • 12
1

For a "repeat until" block the simplest way would be to "or" your normal until condition with the break condition in the until.

By adding an incremeting loop counter variable in the loop you can use a "repeat until" to replicate the function of a "repeat n times" block

By using a "repeat until" block with only your break condition you get the equivalent of a "forever" block

If you need another script/ sprite to trigger the break then a public variable will let you break the loop from anywhere and let a single condition break loops for different sprites.

I'd post an image of the blocks but this is my first reply and the site won't let me!

good luck

sjrg1
  • 11
  • 2
  • You could use [Scratchblocks](http://scratchblocks.github.io) to make a picture. ;) The problem with this is we aren't technically breaking out of the loop from inside the script; we're just looping until some conditional returns true. – Nebula Jun 10 '15 at 15:24
  • If it matters to break out mid loop (an odd thing to need in Scratch), and the last iteration of the loop is not undoable in the post loop blocks, then the _if stop script_ route may be only way to go. Broadcasts aren't pretty but they'd be my fallback over custom blocks which I have found to be less easy to explain to some kids. – sjrg1 Jun 10 '15 at 21:05
1

You can use these few ways to do it...

  • conditional loop

  • stop this script

  • if then else, in the else section, put nothing

I would prefer to use the first method, as it requires less blocks and for the first method, you can still add in code that will be executed after the loop has stopped executing.

Account_2
  • 53
  • 6
  • Do you think you could share some examples? The "if then else" one is particularly interesting since I don't think I've heard of it before, but your answer would be a lot more useful to readers with examples all around :) – Nebula Oct 04 '19 at 14:20
  • Please add an example for each of the methods – Xbox One Aug 14 '20 at 00:31
0

You can make it repeat x times or make it have a certain point where it stops, such as another variable changing.

Otherwise, I don't think there is a wat to do that.

0

Use the repeat until block. Then put in an equals block or whatever into the boolean part. Then inside that repeat until block, put a stop this script block.

Hope this helps :D

The Butler
  • 53
  • 1
  • 8