How to stop a script in the middle when something happens?

Here's an example. Let's say I have an animation for a sprite that goes:
(switch to costume 1)
(wait .1 seconds)
(switch to costume 2)
(wait .1 seconds)
(switch to costume 3)
(wait .1 seconds)

And so on. But if at any point in this script, should I press X, I want it to stop. What I could do is something like this:

_IF (not) x key pressed
(switch to costume 1)
(wait .1 seconds)

_IF (not) x key pressed
(switch to costume 2)
(wait .1 seconds)

_IF (not) x key pressed
(switch to costume 3)
(wait .1 seconds)

_IF (not) x key pressed
(switch to costume 4)
(wait .1 seconds)

But that takes forever and isn't very efficient. What I'm asking for is a better way to do this. Like maybe I could make a block that covers the whole script and says (if X key pressed, break), or something like that.

edit: you can find the solution here

It's not quite exactly what you want (it won't stop until a complete cycle finishes), but you might like it better than a zillion IFs.

You could try this:


But this will stop all the scripts in the sprite (other than the "when X key pressed" script), not just the one script.

Unfortunately, I tried that already. But I don't find it worth waiting through the entire cycle. Thanks though.

untitled script pic (19)


Slightly overkill - clone as a thread/process


untitled script pic (23)

I created a wait (number) seconds block with an alternative condition as well as equivalents for all the other timed functions:


This is similar to what @dardoro was doing.

I think you should consider catch (tag){ } ::control throw [catchtag]::control in the iteration, composition library.

Maybe try something like this?


(Oh yeah, I used @dardoro 's block, as you can see)

If its ok, can you explain the first one? I have no idea how to read it yet.

Snap! can switch to another script only at certain point (wait, end of the loop, for each...)
So you must use some kind of the wait - You may use wait 0.1 / break on condition.

Do you need explanation of the wait/break internals?
It replaces a single wait instruction with the serie of shorter wait to stop early if condition is true (call condition)

Sorry but Im still not clear on the break part and how it works. Thanks for the help btw

stop this script ???
It breaks the whole script/process up to green flag or when receive.
I'm not a native speaker, and maybe it's not clear enough; sorry.