Single frame describes techniques that make scripts run faster than normal. Loop blocks by default iterate once per frame. Scratch targets a frame rate of 30 FPS.
Loop Unrolling
The following script makes the sprite move in a circle by repeatedly moving forwards, and turning clockwise:
when green flag clicked go to x: (0) y: (115) point in direction (90) repeat (72) move (10) steps turn cw (5) degrees end
By eliminating the loop and duplicating the code (called "loop unrolling"), the script can be made to run instantly:
when green flag clicked go to x: (0) y: (115) point in direction (90) move (10) steps turn cw (5) degrees move (10) steps turn cw (5) degrees ... move (10) steps turn cw (5) degrees move (10) steps turn cw (5) degrees
The major downside to this technique is dealing with the possibly large number of duplicate blocks. They can be harder to read, edit, and slow down the project editor.
Run Without Screen Refresh
Custom blocks have a setting called "Run without screen refresh" which makes loops run without delay. Instead of loop unrolling as shown above, the code can be put under a definition hat:
when green flag clicked move in a circle define move in a circle go to x: (0) y: (115) point in direction (90) repeat (72) move (10) steps turn cw (5) degrees end
Turbo Mode
Turbo Mode allows a project to run faster than normal while redrawing the stage at regular intervals. This method can be used when many scripts need to run fast.
Multiple Threads
Some blocks will yield, that is, they pause the current script from running so it can be continued in the next frame. An example of this is the sound volume and effect blocks. These yields can sometimes be undesirable so moving them to separate threads can avoid it.
The following script sets volume in 1 frame, pitch in the next, and pan after it. They do not update all at the same time:
set volume to (. . .::grey) set [pitch v] effect to (. . .::grey) set [pan left/right v] effect to (. . .::grey)
The following uses a broadcast to run the blocks separately. Their yields do not affect other scripts:
broadcast (update sound effects v) when I receive [update sound effects v] set volume to (. . .::grey) when I receive [update sound effects v] set [pitch v] effect to (. . .::grey) when I receive [update sound effects v] set [pan left/right v] effect to (. . .::grey)
All At Once
| This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |
pen down
all at once {
repeat (300)
turn right (15) degrees
move (10) steps
end
} :: control
The All at Once block, which is no longer in Scratch because of the option to run a custom block without screen refresh, evaluated the argument atomically.
Atomicity
Single-frame is related to the concept of atomicity. Atomic scripts run completely through and then show the end result. Most programming languages provide atomic looping only. For learners, though, non-atomic looping is easier to understand. For example, the following script reacts differently in atomic and non-atomic interpreters:
when gf clicked repeat (180) turn cw (1) degrees end
In an atomic interpreter, the script would pause for a second, then the sprite would suddenly turn upside-down. In a non-atomic interpreter like Scratch, the sprite would rotate slowly until it is upside-down.