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.[citation needed]

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
The All at Once block in action.

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. 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. [citation needed]

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.

Cookies help us deliver our services. By using our services, you agree to our use of cookies.