Discuss Scratch

Wowfunhappy
Scratcher
27 posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

Scratch has an “all at once” C-shaped block. https://scratch.mit.edu/projects/788226048. It doesn't do much of anything, and is hidden in the block palette. The source code in scratch-vm provides a pretty good description of why it exists:

https://github.com/LLK/scratch-vm/blob/f2781d3ea50d17e5088eec413935fd639cf3995b/src/blocks/scratch3_control.js#L195

allAtOnce (args, util) {
    // Since the "all at once" block is implemented for compatiblity with
    // Scratch 2.0 projects, it behaves the same way it did in 2.0, which
    // is to simply run the contained script (like "if 1 = 1").
    // (In early versions of Scratch 2.0, it would work the same way as
    // "run without screen refresh" custom blocks do now, but this was
    // removed before the release of 2.0.)
    util.startBranch(1, false);
}

I wanted to make the `all at once` block behave as it did in those early versions of Scratch 2.0, when the contained script would run without screen refresh.

I decided to try:
allAtOnce (args, util) {
    util.thread.peekStackFrame().warpMode = true;
    util.startBranch(1, false);
    util.thread.peekStackFrame().warpMode = false;
}

The results were… interesting.



I had effectively created the anti-all-at-once block: code outside this block was running all at once, while code inside the block ran normally! It was almost as if I had turned warp mode off at the start and on at the end…

…on a hunch, I decided to try doing just that:
allAtOnce (args, util) {
    util.thread.peekStackFrame().warpMode = false;
    util.startBranch(1, false);
    util.thread.peekStackFrame().warpMode = true;
}

This version seems to work perfectly! Code inside the all at once block runs without screen refresh, while code outside the block is unaffected!

This is great, but I'm totally weirded out! Clearly, I don't really understand what this code is doing, and I'm concerned that could lead to unintended side effects down the road.

Does anyone know why Scratch is seemingly executing this function bottom-to-top?

Last edited by Wowfunhappy (Jan. 14, 2023 16:41:52)

CST1229
Scratcher
1000+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

Scratch executes all block functions (that don't return a Promise) all at once (ha) - util.startBranch() doesn't execute the branch right when it is executed, it essentially schedules the branch to start after the function finishes running.

I don't know why that works - it seems like that would make the entire script run without screen refresh (including the blocks after it), or run the script INSIDE the c-slot with screen refresh and run the blocks after it WITHOUT screen refresh…

When I also did this, I handled it by adding an additional argument to the startBranch() function, which passes it to the sequencer's stepBranch() function (via another added argument), which passes it to the thread's pushStack() function (via ANOTHER added argument), which then finally passes the warp mode to the created stack frame.

Last edited by CST1229 (Feb. 3, 2023 18:11:27)


This is a signature. It's a piece of text that appears below every post I write. Click here to learn more, including how to make your own.
RIP assets image hosting. 2013?-2023



Wowfunhappy
Scratcher
27 posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

CST1229 wrote:

Scratch executes all block functions (that don't return a Promise) all at once (ha) - util.startBranch() doesn't execute the branch right when it is executed, it essentially schedules the branch to start after the function finishes running.

I don't know why that works - it seems like that would make the entire script run without screen refresh (including the blocks after it), or run the script INSIDE the c-slot with screen refresh and run the blocks after it WITHOUT screen refresh…

When I also did this, I handled it by adding an additional argument to the startBranch() function, which passes it to the sequencer's stepBranch() function (via another added argument), which passes it to the thread's pushStack() function (via ANOTHER added argument), which then finally passes the warp mode to the created stack frame.

Thank you!
rdococ
Scratcher
500+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

If `startBranch()` pushes another frame to the stack to represent the code in the C block, then the second call to `peekStackFrame()` returns the new stack frame and you're only setting that frame to warp.

Last edited by rdococ (April 14, 2023 18:37:25)

imakegameslol88888
Scratcher
84 posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

because you coded it properly

I respectfully don't know that my forum is a duplicate. The duplicate is probably too old for me to see


This is game.
define among us
say [How do i create among us with HITBOXES TASKS ONLINE, EVERYTHINGGGGGGG!]
dynamicsofscratch
Scratcher
1000+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

deleted becuase my lemon brain just realized that I messed up the entire point of what you we're asking…

Last edited by dynamicsofscratch (April 22, 2023 05:43:05)


aII toasters toast toast, but what happens when there are no longer toasters being produced? will their technology simply become obsolete, with humans moving onto bigger, better things? will toast become a distant memory, written in textbooks of the future as foods us simpler generations ate? who's to say! society is constantly moving, changing, evolving, ideas being built upon, improved upon, theories being proven or disproven. are we but a blip on the timeline? sure, our names may not be remembered, but that's not the point. you can make a change. you can make a difference. you can make the world better, even if you don't know yet. and the first step is to go for it. even if you are afraid of failure. going back to the example of toasters, do you know off the top of your head who invented them? no? have you used one? probably. so, even if you don't remember my name, if I was able to help awnser your question, that is enough. if I was able to help you, even in the slightest way, this could push you to continue with scratch and not give up after the program crashes, and maybe one day learn other programming languages and change the world. everything is a cause and effect reaction, new inventions lead to the technology of the future, and even as the generations of the past are slowly forgotten, their influence lives on to this day, affecting how the world eventually turned out and how it will be for generations to come.

and, without toasters, we wouldn't have toast.


Regards
dynamicsofscratch

Anything above that grey line is a signature!
Also, anything can be put in your signature, (also referred as a siggy) including ads but! You cannot do anything else that violates the community guidelines as, you will be reported and you could be banned/muted.
Computer enthusiast, coder, designer and a offline veteran.
700th post
Wowfunhappy
Scratcher
27 posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

rdococ wrote:

If `startBranch()` pushes another frame to the stack to represent the code in the C block, then the second call to `peekStackFrame()` returns the new stack frame and you're only setting that frame to warp.

I'll admit I had to re-read this many times before I understood it, but—thank you! Combined with what @CST1229 said above about the branch not starting immediately, now the behavior makes complete sense!
julianlukasiak
Scratcher
100+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

Make IT as turbowarp extension.


Support the suggestion HERE by adding this button to your signature

My social media:
Youtube
Snail ide
Planet Minecraft
another acount/inne konto
My turbowarp extensions gallery
Mr_rudy
Scratcher
100+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

julianlukasiak wrote:

Make IT as turbowarp extension.
that isn't possible, it is a block in the VM, if you want to use it in turbowarp just use custom blocks

CHECK OUT SNAIL IDE:
Website
Fourm Post



i make games and other cool stuff

when @greenFlag [clicked v]:: events hat
when (when @greenFlag clicked:: events) clicked:: events stack
when @greenFlag clicked {
when @greenFlag clicked:: events stack
}:: events
when <@greenFlag:: events> clicked:: events
when ({when @greenFlag clicked:: events stack}@addInput:: events) clicked:: events stack
when @greenFlag clicked:: events cap
evil kumquats ate this sentenc-
BigNate469
Scratcher
1000+ posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...

Or you could just find a project that contains the block, download it, upload it to turbowarp, delete everything but the block in question, and you now should be able to use it in turbowarp, just copy and paste the block as necessary.

Highlight any part of this signature and press ctrl+shift+down arrow to see the rest of it
forever
if <person asks [what's a signature] :: sensing> then
Redirect to [https://en.scratch-wiki.info/wiki/Signature] :: motion
end
end
Please read the list of Officially Rejected Suggestions before posting a suggestion for Scratch! 100th post
This signature is designed to be as helpful as possible.
View all of the topics you've posted in:
https://scratch.mit.edu/discuss/search/?action=show_user&show_as=topics
View all of your posts:
https://scratch.mit.edu/discuss/search/?action=show_user&show_as=posts
Forum tips:
Don't post in topics where the latest post is over ~2 months old, unless you have something critical to add. Especially in topics that are several years old- it isn't helpful, and is known as necroposting.
Don't post unrelated things in topics, including questions of your own. Make a new topic for your questions.
You can use the
 [color=color name or hexadecimal value here] and [/color] 
tags to color text.
Lesser-known Scratch URLs:
https://scratch.mit.edu/projects/PROJECT ID HERE/remixtree (replace “PROJECT ID HERE” with project id number. Shows all the remixes of the project, and the remixes of those projects, and the remixes of those projects, and so on, as a chart. Link currently redirects to one of my projects)
View a larger list at: https://scratch.mit.edu/discuss/topic/542480/
Why @Paddle2See's responses are so often identical: https://scratch.mit.edu/discuss/topic/762351/
1000th post
EeveePro2756
Scratcher
4 posts

I made the `all at once` block actually run all at once. My code shouldn't work, I'd like to know why it does anyway...


I'm not sure but try switching versions.

Want to change/make your own signature? Well use the code below! Make sure you use it in your browser's console!
this.document.location = "https://scratch.mit.edu/discuss/settings/" + Scratch.INIT_DATA.LOGGED_IN_USER.model.username

when green flag clicked
say [Hi, I'm Eeveepro2756!] for (2) secs
play drum ( v) for (0.25) beats
play drum ( v) for (0.25) beats
play drum ( v) for (0.25) beats
ask [What's your's?] and wait
if <<(answer) = [Eeveepro2756]>> then
say [Hi Eeveepro2756]

else
say (join (answer) [Is your name? Well Hello!])
end
The end(I like Anime!)
define The end(I like Anime!)


-Eeveepro2756

Powered by DjangoBB