ScratchData LogoScratchData
Back to ngnhc's profile

Execution Order Exception

NGngnhc•Created February 8, 2016
Execution Order Exception
2
2
112 views
View on Scratch

Description

This is based on the project of @TheLogFather https://scratch.mit.edu/projects/18490761/ that demonstrates script execution order. It states that script execution order is based on layering. This project explores the exception to that rule. That is, when a message is sent that activates a script that was already running. New Script Execution Order: Scripts that are not already running are executed from the highest layered sprite, down to the lowest, and then followed by the Stage's scripts. Within a sprite, the scripts that are not already running are executed from the lowest to the highest (in terms of layering within the editor pane). Then, scripts that are already running are executed in the same order they were originally executed no matter their current layer (Sprite or Script). Technical Explanation with the help of the Log Father: What Scratch runs are called threads. It runs these threads based on the order of a thread list. Layering is just the way Scratch determines in what order it will add scripts to the thread list. When you broadcast to a script that is already running, the thread on the list is just replaced as opposed to being removed and added back on. This is what causes these scripts to run in the same order they started in and disobey the layer rule. Follow the instructions below to see this in action: Exploration 1: 1. Make sure the project is stopped. 2. Click on Giga, then Gobo, then Pico, and finally Nano. 3. Click the "Broadcast Message" button. The list "Initial" should read "Nano", "Pico", "Gobo", "Giga", and then "Stage". This demonstrates TheLogFather's explanation of layering determining script execution order. 4. Observe that "Nano" and "Pico" are being added to the list "Forever Loops" every second to demonstrate that their script is still running. "Nano" is always added first because its script started first. (as can be seen by the list "Initial") 5. Now click "Broadcast Message" again. 6. The list "Initial" should now read "Gobo", "Giga", "Stage", "Nano", and then "Pico". Why? Nano and Pico are on top! The reason is that Nano and Pico were still running the script from when the message was broadcasted the first time. This caused their scripts to be executed after all sprites that were not already running that script in spite of their layer. Similar tests with scripts within the same sprite yield parallel results. Exploration 2: 1. Complete steps 1-6 of Exploration 1. 2. Click on Giga. 3. Click the "Broadcast Message" button. 4. The list "Initial" should now read "Giga", "Gobo", "Stage", "Nano", and then "Pico". Giga appeared before Gobo this time because it was on top of Gobo. 5. Click on Pico. 6. Click the "Broadcast Message" button. 7. The list "Initial" should now read "Giga", "Gobo", "Stage", "Nano", and then "Pico". Why? Pico was just moved in front of Nano! The reason is that Nano and Pico were still running the script from when the message was broadcasted the first time. This caused their scripts to run in the same order in which they were originally activated in spite of their current layer. Similar tests with scripts within the same sprite yield parallel results.

Project Details

Project ID97066781
CreatedFebruary 8, 2016
Last ModifiedFebruary 12, 2016
SharedFebruary 8, 2016
Visibilityvisible
CommentsAllowed