Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[5] A little bit of Lua


2026-01-07

A debugger is a tool that allows you to inspect your Lua code as it is run by the Lua interpreter. It is part of your IDE, and helps to see what is happening in your code. It is invaluable for diagnosing bugs, and tracking down problems in larger programs. Here we will use a small program to print out the numbers from 1 - 10 as a way to learn how to use the debugger.

Setting up for debugging

Enter the following Lua program into a new file and save it.

-- Debugging.

local count = 1

while count <= 10 do
  io.write(count .. " ")
  count = count + 1
end

io.write("\n")

In your IDE, you need to enable the Stack window so you can see the values of variables in your program as you debug it. This is a one off thing, and can be done by pressing Ctrl + Shift + S or by navigating to the View menu and clicking on the Stack Window.

This will open a floating window over your IDE. This is not too helpful, so it is best to dock this window on the left hand side of the IDE with the Project and Outline tabs. Go ahead and drag the stack window to those tabs. Your IDE should now look like this:

Docked stack window

Setting a breakpoint

To debug our program we need to tell the debugger where in our Lua code we want it to stop execution. We do this by setting a breakpoint on the line we want to debug from. Do this by clicking on the line you want to set the breakpoint on (in our case the line that says local count = 1). Then click the red circle icon on the toolbar. This should make a big red circle appear next to the line number in margin like this:

Breakpoint set

You are now ready to debug your program!

Stepping through the code

Click the single green arrow on the toolbar (or press F5 on the keyboard). This will start your program with the debugger running. Execution of your program will stop on the lien that has the breakpoint on it. From here you can look at the contents of variables in the Stack Window. You can advance the debugger by one line by clicking on the Step into icon on the toolbar (or pressing F10 on the keyboard). As the program advances line by line, a green arrow will show the current line to be run next.

Debugger in action

You can set as many breakpoints as you like in a program. Using the icons on the toolbar, you can step over lines, step into functions (we'll cover functions later), run to the cursor, and start running the program again.

Exercises

  1. Practice using the debugger. Open up some of the previous lessons programs and set breakpoints somewhere in the code. Get used to seeing values in the Stack Window, and stepping over the code line by line. This is your new super power when it comes to finding bugs in your programs.
end