You are on page 1of 13

Unit 3 – Programming (FO)

Semester 2 – Week 9
Debugging Scripts using Scratch

Source: http://wiki.scratch.mit.edu/wiki/
Scratch Debugging
• A common problem for Scratchers is when a script that may seem to be flawless ends up
not doing what is expected, or getting highlighted in red. These are usually normal human
errors and can be fixed.

• We will be discussing the following ways to debug scripts using Scratch:


• Inspecting Scripts
• Preventing Errors
• Checking Values
• Testing Code
• Single Stepping Debugging
Inspecting Scripts
• Inspecting a script that does not work is often not easy. Finding a problem with a script you
have written is hard because it makes sense to the writer. Often, thinking as if you are the
computer will highlight the issue.

• For example, to find the sum of the fractions 1/2+1/3+1/4+…+1/100 one may use the
following script:

This will not work, because on


inspecting the script, we see that
the "n" variable will be equal to 0 on
the last iteration (loop/repeat), and
will cause a zero division error.
Preventing Errors
• The best way to prevent silly mistakes is to have neat, organized code. Order scripts by
their functions or order of evaluation will help find mistakes and prevent them.

• Leave comments wherever you had to spend time to get something to work right; this
way, if it breaks again, you can refer back to what worked before. Anyone with enough
experience should be able to understand your script at first glance.

• Do not blindly copy scripts from other projects, unless that was the intention of the
project. Other than moral copy theft issues, chances are the script will not work in the
context of your program. 
Checking Values
• If a value needs to be known in order to determine whether or not a script is
functioning, there are a few ways to find it, such as:

• Using the Say ( ) block


• Updating a Variable
• Using a Boolean
Using the Say ( ) Block
• The ‘say’ block can be used to constantly check a value.
Updating a Variable
• A variable can also be used to check and update constantly.
Using a Boolean
• To check whether a condition is correctly resulting to true or false, we will need
to use a Boolean:
Testing Code
• Testing a large, complex project is daunting, because every change can potentially
wreck something.

• Often, the best way is to simply run the project again and again.

• However, large games or animations would be painful to debug like this. Instead, you
can introduce certain testing codes, which can be removed in the final release. For
example, to test level 27 of a game, add a script where when "space" is pressed, the
game goes to the next level.
Single Stepping Debugging
• Single Stepping Debugging is a feature that was available on Scratch 1.4 and is no longer
available on Scratch 3.0.

• Instead of Single Stepping Debugging, Turbo Mode is used in Scratch 3.0.

• Single Stepping is a tool in Scratch that runs scripts slower or faster, and performs


each block individually.
Single Stepping Debugging
• By clicking on "Start Single Stepping" it starts the process with the current
option. By default it is "Flash Blocks (Slow)". These are all the possible options
in can be set it to:
• Turbo Speed
• Normal
• Flash Blocks (Fast)
• Flash Blocks (Slow)
Single Stepping Debugging
• Turbo Speed: In turbo speed, all scripts run at extreme speed. This is very useful for
speeding up mathematical projects, however makes features such as graphic effects and
clicking on sprites lag quite a bit.

• Normal: Just runs all the scripts as they would normally be; it's just like having Single
Stepping turned off.

• Flash Blocks (fast): Runs a project at normal speed, however, when a block is active, it
turns yellow. This is useful for debugging.

• Flash Blocks (slow): Runs a project extremely slowly, and turns blocks yellow when they
are active.
Uses for Single Stepping Debugging
• Single Stepping Debugging can be useful for:

• Speeding up mathematical projects


• Finding the source of a glitch
• Finding out exactly how a project works

You might also like