Discuss Scratch

MissNizio
New Scratcher
2 posts

"When green flag is clicked" - which goes first?

Hi there,

Totally n00b question for everyone about “When green flag is clicked” and the execution of script blocks:

If I have 3 sprites with “When GF is clicked” events, and one of those sprites has “When GF is clicked” events, what will be called first? And what if I have forever loops in one of these “When GF is clicked?” What happens?

Being that I'm used to coding in Java and having a “Main” program, I am really confused about what order everything executes in. I've tried adding in “wait until space key is pressed” blocks, and I'm still having a tough time figuring it out. It seems like some sprites don't even execute when the space key is pressed in some situations, meaning that the “When GF is clicked” might activate only up to the pause.

Can anyone help me by sending me to a link that explains this?

Thanks in advance!
MiniFluffyBug
Scratcher
500+ posts

"When green flag is clicked" - which goes first?

I don't really get what you're saying, but I may be able to help. If you want to wait until a button is pressed, instead of the green flag, then put this:
when [space v] key pressed
Sorry if this isn't what you're trying to find out. I couldn't really understand what you were trying to say. If this isn't what you want to know, please tell me and try to explain it to me, as I don't working with Java, in words at least. Scratch blocks are Java Script, by the way.

Last edited by MiniFluffyBug (Feb. 25, 2014 17:25:19)

Paddle2See
Scratch Team
1000+ posts

"When green flag is clicked" - which goes first?

All “When GF is clicked” scripts are supposed to start simultaneously - but since Scratch isn't really multi-threading, this doesn't really happen. I don't know which one is starting first - and you probably shouldn't count on any particular one starting first. I often add in Wait blocks to some of my GF scripts to make sure that they don't start first - maybe to give an initialization block on another script time to set a value.

As for your attempt to delay execution until the space bar is pressed, that should work. Unless your project has lost keyboard focus (like maybe you are using Ask/Answer blocks to gather input from the user.

If you haven't experimented with Broadcast Messages, you should really do that. It makes sequencing stuff much easier. Have a script do task A that ends with a message being sent that starts task B. Make sure you understand the difference between “Broadcast” and “Broadcast and Wait”. Hope that helps
MissNizio
New Scratcher
2 posts

"When green flag is clicked" - which goes first?

MiniFluffyBug: I'm not really concerned controlling the “wait until space key is pressed” blocks. I was doing that to try and see what order Scratch was executing code.

Paddle2See: Thanks! Yeah, it is a questions of when the “when GF is clicked” actually start. As far as my computer science minor tells me, multiple lines of code cannot really be read at the same time, but can be read quite quickly. Knowing that Scratch isn't really multi-threading helps. I'm just trying to figure out what order everything executes in so I can help a couple students of mine figure out how to debug code they are trying to remix for their own projects.

If anyone has any more suggestions, please throw them at me!
drmcw
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

All the green flag scripts will be set to run. I don't know of any guaranteed method of deciding what order the scripts will run, however each script will run either until it ends OR hits a wait type block such as wait, play sound until done, broadcast and wait etc, OR the script has performed one iteration of a loop such as repeat or forever. Once one of these stop or yield conditions are met then it's onto the next script. Once all scripts have been run until ended or yielded then Scratch will wait until the next screen update (30 per second) unless the scripts have made no changes to the screen in which case they will run again by the same rules.
DadOfMrLog
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

The order that the scripts get run depends upon several things…

However, I'd really recommend that you don't rely upon any defined execution order. Instead, I would suggest that you have only a single “when GF clicked” entry point, and ensure your scripts execute in exactly the order you want in the different sprites by using broadcasts (and that may well mean broadcast-and-wait in many cases, though you can chain together a set of scripts in different sprites to execute one after the other just by using broadcast at the end of each, to start up the next, if you want).

(But if you really do want to know about script execution order, let me know, and I can explain how it currently works…)

Last edited by DadOfMrLog (Feb. 25, 2014 19:52:26)

Deerleg
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

Well, they should seem to run at about the same time.
turkey3
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

MissNizio wrote:

MiniFluffyBug: I'm not really concerned controlling the “wait until space key is pressed” blocks. I was doing that to try and see what order Scratch was executing code.

Paddle2See: Thanks! Yeah, it is a questions of when the “when GF is clicked” actually start. As far as my computer science minor tells me, multiple lines of code cannot really be read at the same time, but can be read quite quickly. Knowing that Scratch isn't really multi-threading helps. I'm just trying to figure out what order everything executes in so I can help a couple students of mine figure out how to debug code they are trying to remix for their own projects.

If anyone has any more suggestions, please throw them at me!
Either way, the same amount of blocks will really be run from the scripts until the screen refreshes. And I think if you have a dual-core or more processor some things can run simultaneously, but not sure if Scratch fully utilizes that,
TheLogFather
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

FYI, I decided I might as well make a project which demonstrates the order that scripts get executed - both within and across sprites:

http://scratch.mit.edu/projects/18490761/

Enjoy!

(But, still, the better way is to just have a single entry point, and control the flow yourself via broadcast and waits, since it's hard to ensure your scripts and sprites will be ordered in exactly the way you want when the project gets started…)

Rickdangerous
New Scratcher
38 posts

"When green flag is clicked" - which goes first?

Hi everybody!

I was doing some simple tutorial, to see how things work, when I got a question about the order of events. In the tutorial, about random apples falling from sky, the script was this:
———————
comand line 1) When is clicked:
comand line 2) Go to
comand line 3) Set up Y to
———————
My question is: if the apple goes to a random position first, how can we set the Y position after that? Unless that comand line 2 and 3 occurs at the same time, but still I dont get it, because when put things like this:

———————
comand line 1) When is clicked:
comand line 2) Set up Y to
comand line 3) Go to
———————

then, the apple just gets to a random position…

So, can someone explain me the order of events and comands on scratch? It doesnt look like so straight as I tought. Thanks
Wahsp
Scratcher
1000+ posts

"When green flag is clicked" - which goes first?

Rickdangerous wrote:

Hi everybody!

I was doing some simple tutorial, to see how things work, when I got a question about the order of events. In the tutorial, about random apples falling from sky, the script was this:
———————
comand line 1) When is clicked:
comand line 2) Go to
comand line 3) Set up Y to
———————
My question is: if the apple goes to a random position first, how can we set the Y position after that? Unless that comand line 2 and 3 occurs at the same time, but still I dont get it, because when put things like this:

———————
comand line 1) When is clicked:
comand line 2) Set up Y to
comand line 3) Go to
———————

then, the apple just gets to a random position…

So, can someone explain me the order of events and comands on scratch? It doesnt look like so straight as I tought. Thanks
The order is from top to bottom. In the future, please make your own topic so more people can respond to you

Powered by DjangoBB