Debugging Lua scripts using RemDebug
Probably the easiest method to implement is the print statement. If one actually calls putting print statements into scripts “debugging” is a different question, but it helps. It can sometimes be hard to figure out what value you were actually looking for and often results in add print statemnt -> run -> adjust -> run -> adjust -> run …. The way it works is simply adding print(yourvalue) to your script:
function bar() for i = 1, 10 do tab.foo = tab.foo * i print(tab.foo) end end
This is a really simple example but getting values to be printed helps when debugging a problem, though in this example might result in a confusing list of numbers.
On the hunt for a debugging solution for the command line you will probably come across the remote debugger for Lua. After downloading and installing it (you can use LuaRocks for it), there a 2 steps to be taken
Start the controller
Add this to the script you want to debug:
Run your lua script in a different console than the controller
You will notice that the controller stop and gives you an input while your script is being stopped:
Lua Remote Debugger Run the program you wish to debug Paused at file home/qs/luarning/debug/file2debug.lua Type 'help' for commands
The next thing you have to do is tell the controller what its base directory is (where it can find the files you are going to debug):
Note: On Linux when you enter your basedir, do not put a leading slash in front of the path. I haven’t figured out why that is yet, but using “/home/qs/...” did not work for me on Linux (Ubuntu 9.04).
For a list of the available commands one can use the 'help’ command in the controller console window. The ones I have used most:
setb file2debug.lua 12 --sets a breakpoint in the specified line of the specified file delb file2debug.lua 12 --removes the breakpoint delallb --removes all breakpoints run --run the file until the next breakpoint eval <exp> --returns the value of the given expression (e.g. eval tab.foo)
RemDebug enables you to set expressions, too, instead of just evaluating them. This makes it a really nice tool to use.
Nothing can stop you when you are good at debugging. And I mean it. As soon as you are able to debug, you start to learn how the program works or how the programming language works / handles things. You get to study the program / script step-by-step. So start debugging your scripts in case there is a bug or you just want to get to know the code you are working with. There are still occasions where you can use print statement, but I think RemDebug is far more powerful.