Single Frame
Single frame is a way to make a script run faster than normal.
Scratch loop blocks are programmed with a slight delay between repetitions. If this delay did not exist, simple motion scripts could cause a sprite to fly or even disappear off the screen instantly. The control blocks and motion blocks have this delay. During this delay, an additional screen refresh is done: Scratch redraws the entire Stage. This is also a big source of lag.
The Single Frame technique can be used to bypass this slight delay, so that computationally-heavy tasks (for example, Raycasting or Raytracing, or complex string manipulation and parsing) run quickly. It is like the turbo mode function. The easiest way is to remove all of the blocks mentioned above so that the screen is refreshed only once at the end of a script: all drawing happens in a "single frame".
Example
For example, the following scripts perform the same task in essence, but if run, in the second script the sprite will flicker while in the first it will remain still:
when green flag clicked go to x: (100) y: (0) forever // single frame set x to ((x position) * (-1)) set x to ((x position) * (-1)) set x to ((x position) * (-1)) set x to ((x position) * (-1)) set x to ((x position) * (-1)) set x to ((x position) * (-1)) when green flag clicked go to x: (100) y: (0) forever repeat (6) // not single frame set x to ((x position) * (-1)) end
By performing the action 6 times directly, rather than using the repeat block, the script works slightly faster and thus the flickering is gone.
Single Frame works in the Scratch 3.0 Online Editor and Offline Editor. It may not work on older versions of Scratch.
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.
Atomic Procedures
Custom blocks can be made to run atomically. In Scratch 2.0 and later versions, checking the "Run Without Screen Refresh" box in a custom block's edit menu sets the custom block to run atomically. 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, however, but still had the built-in lag.
pen down all at once { repeat (300) turn right (15) degrees move (10) steps end } :: control
Problems
Using the single-frame technique can/will slow down scripts in other loops, as Scratch tries to run through every loop at the same rate (fast loops generally will not begin their second repetition until the slowest loops are done). As a result, scripts that may have been faster before the implementation of the single-frame technique is applied to one of the loops will usually run slower.
Another problem is the readability of scripts. Complex single-frame projects often have large scripts which are hard to understand. Also, Scratch sometimes cannot draw large scripts properly, and cuts off the bottom few blocks of the script, and/or causes massive lag.
Larger scripts are harder to edit as the Squeak environment slows down. A lag of many seconds can develop in enormous scripts, making edits tough. Mainly adding blocks, dragging out blocks, changing input values, and adding comments gets delayed.
It is impossible to make a Forever block single-frame. Placing a forever loop in an atomic custom block will cause your project to lag severely. Шаблон:Cn
Tips
Some tips to make the process of making single-frame projects easy are:
- Use the duplicate function. If you need to single-frame a repeating script, duplicate the script that needs to be repeated the appropriate number of times by right-clicking the top block of the script and selecting "duplicate".
- Try to avoid manually changing values in blocks as it is time-consuming. For example, if a value needs to be continually incremented each repetition, it is faster to replace it by a variable and add a "change variable by" block at the end of each repetition.
Effects of Turbo Mode
Turbo Mode runs a project without the slight delays inherent in loop blocks, and redraws the stage at regular intervals. Using this method and then running a project in turbo mode decreases the effectiveness of turbo mode, when compared with the same project running at normal speed. The single-frame technique can often be used to allow complex projects to run without the aid of turbo mode.
BYOB Single Frame
The Scratch Modification BYOB allows atomic procedures. To make a procedure (custom block) atomic in BYOB 3.1 and below, check the "atomic?" checkbox in the block editor dialog. In Snap!, use the warp block to make the enclosed script atomic when run.
In both versions, recursion is atomic. So an atomic repetition block can be written, which takes a lambda input and recursively evaluates the lambda and then evaluates itself, decrementing the number of repetitions: