Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Finding bugs

Sometimes a game doesn’t stop with an error. It just doesn’t do what you meant. The best way to find out why is to stop it and look. Here’s a game with a bug in it:

SCREEN 320, 240
lives = 3
FOR wave = 1 ... 5
    hit = wave MOD 2
    IF hit THEN lives = lives - 1
    PRINT "wave", wave, "lives", lives
END FOR
IF lives > 0
    PRINT "You win!"
ELSE
    PRINT "Game over"
END IF
wave 1 lives 2
wave 2 lives 2
wave 3 lives 1
wave 4 lives 1
wave 5 lives 0
Game over

Three lives, five waves, and you lose. Why? Let’s look.

Breakpoints

Click in the gutter to the left of a line’s number, or put the cursor on the line and press F9. A red dot is a breakpoint: when the game gets to that line, it stops, before the line runs.

Put one on IF hit THEN lives = lives - 1 and press Run. The game stops there, the line is lit up, and the status says where it’s paused.

Looking around

The Debug tab shows what’s going on at that moment:

  • Variables: every variable and its value, under Locals (the ones in the function you’re in) and Globals.
  • Call stack: which function called which to get here. Click one to see where it was.
  • Threads: every thread the game is running.
  • Watch: type an expression, like lives > 0, and it’s worked out every time the game stops. × stops watching it.

Here, wave is 1 and hit is 1. So the very first wave is a hit, and there’s the bug: wave MOD 2 is 1 for every odd wave, and three of the five are odd.

Another game, stopped at a breakpoint: the line is lit up, the Debug tab shows every variable, and the watch x > 50 is worked out each time it stops.

One step at a time

When the game is stopped, the buttons by Continue move it on:

ButtonKeyWhat it does
ContinueF5runs on, to the next breakpoint
Step overF10runs this line, and stops on the next
Step intoF11goes into the function this line calls
Step outShift+F11runs to the end of this function, and stops where it was called
Pausestops the game wherever it is, while it’s running

Press F10 a few times, and watch lives count down in the Debug tab.

Clever breakpoints

Right-click a breakpoint for more:

  • Condition: it only stops when this is true, like lives = 1.
  • Skip visits: it lets the game past this many times before it stops.
  • Log message: it doesn’t stop at all. It prints the message to the Console instead, with values in braces, like wave {wave}: {lives} lives.

A log message is like a PRINT you didn’t have to put in your code, and don’t have to remember to take out.

Crash-tan’s Tip: You can change a variable while the game is stopped. Type lives = 10 in the Console, press Continue, and see whether the rest of the game works with more lives.

Good to know

  • The Debug tab shows the variables where the game stopped. To see a value in the function that called this one, watch it, or step out to it.
  • Breakpoints remember a file by its name, so two files with the same name share their breakpoints.
  • When one thread stops, the game’s other threads carry on.