DocumentInQuestion.png This page or section is in question as to whether it is useful or not. You can discuss whether you think it should be deleted or not on its talk page. (April 2024)
Reason: Seems overly specific for its own article
Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (April 2024)

Speedcode is when programmers have a loop which runs too slow, so they define a script, drag the contents of the loop into the new function and then add an amount of that newly created function where the script intended to run, making the code run faster. Although it makes the code more complex and hard to read, it works. This tutorial will show how to create them.

Workings of Speedcode

Speedcode works because it saves CPU cycles, and it is also commonly referred to as 'untying a loop',[citation needed] as instead of using a loop, your simply putting all inside your script in a function, then copy and pasting the function multiple times where the loop would be.

Creating Speedcode

An example of when speedcode is required is when a game's game loop is too slow. Create a new project then create this script in Sprite1 (Scratch Cat):

when green flag clicked
go to x: (0) y: (0)
set [gravity v] to (-1)
forever
Step Forward in Simulation
end

define Step Forward in Simulation
change [gravity v] by (-0.2)
if <(y position) < (-130)> then
  go to x: (0) y: (-130)
  set [gravity v] to (0)
end

change y by (gravity)
when [space v] key pressed
change (gravity) by (5)

Run it, and one will notice that the cat fall slowly. Look at the script starting with 'when green flag clicked', and add another 'Step Forward in Simulation' block below the first one. It fell down twice as fast, because instead of running it once per frame, it ran twice per frame. It is possible to make it even faster by adding running the script more each frame, like so:

when green flag clicked
repeat (5)
  say (loop)
  change (loop) by (1)

turns into

when green flag clicked
loop
loop
loop
loop
loop

define loop
say (loop)
change (loop) by (1)
Cookies help us deliver our services. By using our services, you agree to our use of cookies.