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. This is commonly known as debugging which originated when Grace Hopper in the early 20th Century got a bug out of her computer.
Inspecting a script that does not work is often not easy. Finding a problem with a script you have written is difficult 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:
set [n v] to  set [total v] to  repeat until <(n) < (1)> change [n v] by [-1] change [total v] by ((1) / (n)) end
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.
Other issues can occur when actions do take place, but are not seen by the user. For example if a sprite is hidden or has a ghost effect of 100. A practical example is a continuation of the script above:
say [I have found the sum!] say (total) say [Wasn't that great?]
The script will just say "Wasn't that great", because the previous two actions happen in a split second, or sometimes, within a single render cycle of the evaluator. To fix these problems, you should use Single Stepping, which slows down all scripts.
Sometimes, the error is purely logical, and appears sensible only at the moment. These are often the hardest to track down, because there is no true "error". For example, accidentally inserting the wrong variable, or changing the order in subtraction will mess up a script without giving a trace. The best way to find these mistakes is to ask a friend. Walk through the entire program, block-by-block, and make sure your friend understands each step. If there is a logical mistake, he or she will probably catch it.
- 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 copytheft issues, chances are the script will not work in the context of your program. To learn how to make easy, integratable tools, see Advanced Clone Usage.
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.
Using the Say () block. The say block can be used to constantly checking a value.
forever say (((var1) + (var2)) * (var3)) //Outputs the value of (var1 + var2) * var3 end
Updating a variable. A variable can also be updated constantly.
forever set [check v] to (((var1) + (var2)) * (var3)) //Outputs the value of (var1 + var2) * var3 end
These methods can also be used to check a boolean:
forever say <touching [Sprite1 v]?> //Will say whether or not the sprite is touching Sprite1 end
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.
To make sure your code remains functional, automate certain tests. For example, scripts that should return a numerical answer can be automatically tested against some built-in cases:
define get cube of (5) //imagine this function was implemented if <not <(result) = (125)>> then say [Oops!] for (5) secs say [error with input 5] for (5) secs stop [all v] end define test CUBE with (n) expect (k) //You can even automate this block get cube of (n) :: custom if <not <(result) = (k)>> then say [Oops!] for (2) secs say (n) for (2) secs stop [all v] end when gf clicked test CUBE with (1) expect (1) :: custom test CUBE with (2) expect (8) :: custom test CUBE with (3) expect (27) :: custom
Common causes of errors
- Main article: Script#Script Errors
Often, Scratchers write programs with a script similar to the following:
set x to (10) forever change x by (-1) say ((1) / (x position))
This script will cause an error after the 10th repeat of the forever block, when x = 0, as nothing can be divided by 0 in Scratch 1.4. (Division by 0 actually should return infinity, because to make any number out of 0s, you would need infinite 0s.) When this occurs, the script will be highlighted in red and the ()/() block will turn red. To counter this error, the best way would be to start with x = 10.0001 rather than 10. This way, x will never reach 0, it will hit 0.0001 and -0.9999.
Similar problems occur with square roots, logarithms, and trigonometric functions:
set x to (10) forever change x by (-1) say ([sqrt v] of (x position))// this will error when x = -1. say ([sqrt v] of ([abs v] of (x position)))// this will not error as abs x is always positive.
Order of Commands
The most common problem for Scratchers is for the commands to be in the wrong order. For example, to make a sprite lunge at another sprite, a Scratcher uses commands:
point in direction (45 v) wait (0.5) secs glide (2) secs to x: (54) y: (0) point in direction (90 v)
but it does not look like the sprite is lunging.
A good solution for this problem is to just move around the commands until the script works. Ex., to make the sprite lunge, the Scratcher moves the "point in direction 90" command to right after the "wait 0.5 secs" command, thus making the sprite look like it's lunging:
point in direction (45 v) wait (0.5) secs point in direction (90 v) glide (2) secs to x: (54) y: (0)
Another common problem for Scratchers is the timing of commands. This is often seen in projects that involve 2 scripts cooperating without broadcasts or variables, such as in the sample project Joke. When timing is not perfect the entire project could be thrown off. But do not worry, here are some ways to solve that problem:
Broadcasts and Variables
Using broadcasts and variables will allow the scripts to coordinate with each other. Broadcasts involve hat blocks, so be prepared for a lot of scripts. Using variables will definitely save you scripts. If you use variables in a project like Joke, you'll probably be using the "wait until" command. Otherwise you might be using the "if" command or possibly the "repeat until" command. Either way, the scripts will be more accurately coordinated.
If you really do not want to use broadcasts or variables you could just adjust the amount of time between commands. Use the "wait () secs" command. This would be easy for a project like Joke but not for a complicated game. Other methods recommended.
This method is mainly for very complicated games. To make an accurate detection you might want to repeat a test for a few times before activating a reaction. It may be unnoticeable but it takes a small amount of time to run a command. If you repeat a test command, it will give the detection a little bit longer to change itself. You can see this by downloading this project.
You may also make commands revolving around the timer. Scratch's timer can be reset and manipulated, so you can do a lot with it.
The third and least common reason for scripts not to work would be that the commands just do not work with each other. When this happens, the script will be highlighted in red. The sprite is "stumped". When this is the case there really isn't anything to do about it except to rebuild the script completely, with a different approach.
when gf clicked say [hello] for (2) secs if <visible?> then say [I see you] for (1) secs end
- Main article: Obsolete Blocks
When using older versions of Scratch, you may find blocks labeled "obsolete!". These are usually blocks in the newer versions of Scratch, and are not compatible with older versions. Using these blocks in the older versions will often cause the script to fail.
when gf clicked create clone of [myself v] reset timer
when gf clicked obsolete! reset timer
Blocks like "say nothing" are hidden in Scratch 1.4, but will not cause a script error.