Discuss Scratch

primexandy
Scratcher
56 posts

[Answered] Concurrent list writing

Hi,

Is it necessary to prevent one sprite from writing to a list when another sprite may also be trying to write to it?

For example I have made my own block that looks to see if another sprite is writing to a list. My block waits until the other sprite has finished that operation.

ie:

wait until access = 1 (loop here doing nothing)
set access = 0 (tell others we are writing)
replace item in list with thing (do the work)
set access = 1 (tell others we have finished writing)

Last edited by primexandy (April 24, 2017 11:13:14)

TheLogFather
Scratcher
1000+ posts

[Answered] Concurrent list writing

Scratch is single-threaded so it is only ever executing one script at a particular time.

Each currently running script is allowed to run until it reaches a “yield point”. That's either the end of a pass of a loop (well, more precisely, the start of the next pass), a wait block of some form, or the end of the script. Then the next one in Scratch's list of scheduled scripts is allowed to run until its yield point, etc. (Non-refresh custom block scripts are an exception w.r.t. yield points – Scratch will run such a script until it ends, or until it reaches the half second non-refresh limit, not allowing any other scripts to run until then.)

Once all scripts in the schedule list have reached their yield points, Scratch allows Flash to do a screen refresh (if it had encountered a block that potentially changed something on screen, and if not in turbo mode).

You don't have to worry about two sprites/clones/scripts trying to do things at the same time. The only thing you *do* have to worry about is if things need to happen in a particular order – so one script needs to finish before another starts (e.g. some variable/setting gets initialised in one script before another script makes use of it in a loop, say).

Hope that helps!

Last edited by TheLogFather (April 24, 2017 11:09:18)

primexandy
Scratcher
56 posts

[Answered] Concurrent list writing

TheLogFather wrote:

Scratch is single-threaded so it is only ever executing one script at a particular time.

Each currently running script is allowed to run until it reaches a “yield point”. That's either the end of a pass of a loop (well, more precisely, the start of the next pass), a wait block of some form, or the end of the script. Then the next one in Scratch's list of scheduled scripts is allowed to run until its yield point, etc. (Non-refresh custom block scripts are an exception w.r.t. yield points – Scratch will run such a script until it ends, or until it reaches the half second non-refresh limit, not allowing any other scripts to run until then.)

Once all scripts in the schedule list have reached their yield points, Scratch allows Flash to do a screen refresh (if it had encountered a block that potentially changed something on screen, and if not in turbo mode).

You don't have to worry about two sprites/clones/scripts trying to do things at the same time. The only thing you *do* have to worry about is if things need to happen in a particular order – so one script needs to finish before another starts (e.g. some variable/setting gets initialised in one script before another script makes use of it in a loop, say).

Hope that helps!


Thank-you. That's pretty much as I suspected but wasn't sure. It means I can remove a load of checking code.

Cheers

Powered by DjangoBB