Discuss Scratch

legendary34678
Scratcher
1000+ posts

Concurrency in Scratch

Hello,

I'm working on a project that involves two different scripts running concurrently. I want to make sure that there will not be any conflict between the two, so I read this article in the Scratch Wiki to learn more about how Scratch chooses the order in which to run blocks. However, the article only says that "control only moves from one script to another at certain points.“ Does anybody know what is meant by ”certain points"?

Thanks in advance.
BigNate469
Scratcher
1000+ posts

Concurrency in Scratch

Yield points- when Scratch waits a frame (or more) to continue executing a script can change what order scripts are executed in.

These are created by broadcast blocks and wait blocks, anything that takes more than 1 frame to execute (such as a glide block or some of the music blocks).

Preferably you design your project so that it only needs to run one thing at once.

Last edited by BigNate469 (Feb. 21, 2025 22:05:57)

legendary34678
Scratcher
1000+ posts

Concurrency in Scratch

I see. So, if I understand correctly, Scratch will run as much of a script as possible until a yield point is reached. Then, it will check to see which script it should run next at which point control might move to a different script.
BigNate469
Scratcher
1000+ posts

Concurrency in Scratch

legendary34678 wrote:

I see. So, if I understand correctly, Scratch will run as much of a script as possible until a yield point is reached. Then, it will check to see which script it should run next at which point control might move to a different script.
I don't think that's entirely true, although it is possible considering that by far the easiest way to initially choose what order to run blocks in is to follow the order they're in in the project.json file, and there scripts can be listed one at a time.
davidtheplatform
Scratcher
500+ posts

Concurrency in Scratch

Yield points also happen at the end of loops. They don't happen at all if you have a custom block with “run without screen refresh” on.
I would recommend reading this document by RokCoder which explains how scratch chooses to run things in detail.

If you want two scripts to run at the same time without slowing down the project, each one should only have one yield point (probably at the end of a loop):
// vv good vv
forever
move (5) steps
turn cw (15) degrees
// there is a yield point after this block, since it's at the end of a loop block. The other blocks don't cause yield points so this loop runs at 30fps.
end

// vv bad vv
forever
move (5) steps
broadcast [a v] and wait // "broadcast and wait" causes a yield, and the forever block causes another yield, so this loop will only run at 15fps instead of 30fps.
end

// vv bad vv
forever
repeat (10) // this repeat block causes 10 yield points to happen (one for every loop iteration), on top of the one caused by the forever block.
move (5) steps
turn cw (15) degrees
end
end

// The above loop would run at 30/11 = ~2.73 fps.

// vv fixed version vv
define loop // make sure you turn on "run without screen refresh" when creating the custom block, so it doesn't cause any yield points
repeat (10)
move (5) steps
turn cw (15) degrees
end

forever
loop :: custom // the only yield point here is from the forever loop, so it will run at 30fps.
end

Powered by DjangoBB