Discuss Scratch

Stegie1234
Scratcher
100+ posts

Do scripts actually run simultaneously?

Say there are two scripts, functioning kind of like for loops in JavaScript or Python:
when green flag clicked
forever
set [i v] to (1)
repeat (10)
some code
change [i v] by (1)
end
end
when green flag clicked
forever
set [i_2 v] to (1)
repeat (5)
some code
set [i_3 v] to (1)
repeat (10)
some code
change [i_3 v] by (1)
end
change [i_2 v] by (1)
end
end
Variable i_2 is used to avoid affecting variable i in the first script. That is, assuming the two scripts are running at exactly the same time, which is, as far as I know, impossible in many (if not all) languages such as JavaScript which Scratch is programmed in. So, is the extra variable necessary? Or do the two scripts shown above function exactly the same as this one, which would mean only variables i and i_3 (which would probably be renamed to i_2) are needed
when green flag clicked
forever
set [i v] to [1]
repeat (10)
some code
change [i v] by (1)
end
set [i_2 v] to (1)
repeat (5)
some code
set [i_3 v] to (1)
repeat (10)
some code
change [i_3 v] by (1)
end
change [i_2 v] by (1)
end
end
Also, I am not sure if I have posted in the correct forum, but this doesn’t seem like it should be in the help with scripts forum. If I’m wrong, please tell me and I’ll post this in the right forum.

Last edited by Stegie1234 (July 18, 2023 08:14:29)

Jonathan50
Scratcher
1000+ posts

Do scripts actually run simultaneously?

Seems like a loop yields every iteration so other threads get a chance to run (except if turbo/run-w/o-screen-refresh, then it can carry on a certain time). So your loops would probably be interleaved, and both i and i_2 are needed.

Scripts run simultaneously. Simple primitive blocks don't unless a block is waiting for something.
Stegie1234
Scratcher
100+ posts

Do scripts actually run simultaneously?

Jonathan50 wrote:

Seems like a loop yields every iteration so other threads get a chance to run (except if turbo/run-w/o-screen-refresh, then it can carry on a certain time). So your loops would probably be interleaved, and both i and i_2 are needed.

Scripts run simultaneously. Simple primitive blocks don't unless a block is waiting for something.
So if both run w/o screen refresh, is i_2 unnecessary, or have I misunderstood your reply?
Spentinium
Scratcher
100+ posts

Do scripts actually run simultaneously?

Stegie1234 wrote:

Jonathan50 wrote:

Seems like a loop yields every iteration so other threads get a chance to run (except if turbo/run-w/o-screen-refresh, then it can carry on a certain time). So your loops would probably be interleaved, and both i and i_2 are needed.

Scripts run simultaneously. Simple primitive blocks don't unless a block is waiting for something.
So if both run w/o screen refresh, is i_2 unnecessary, or have I misunderstood your reply?
If they run with screen refresh, then you need 2 variables.
If they run without screen refresh, then you can put it into one script / you only need one variable.
Jonathan50
Scratcher
1000+ posts

Do scripts actually run simultaneously?

Stegie1234 wrote:

So if both run w/o screen refresh, is i_2 unnecessary, or have I misunderstood your reply?
Yes, if the loops complete in a short enough time, although it'd probably still be clearer to use all 3 variables
Jonathan50
Scratcher
1000+ posts

Do scripts actually run simultaneously?

Here's a demo – https://scratch.mit.edu/projects/875678483/

Though a much simpler demo would just be something like
when gf clicked
repeat (100)
move (10) steps
end
in multiple sprites; the difference is just that code involving graphics etc. runs at 30 FPS by default; if there aren't any blocks like that, it'll run a lot faster but other scripts still get a chance to run after each loop iteration.

Edit: That also means you can write atomic sections, so it prevents race conditions (I added two examples to the project where two scripts use the same variable; only the example with an empty REPEAT 1 has a race condition)

Last edited by Jonathan50 (July 18, 2023 23:41:04)

Powered by DjangoBB