Defold contains an integrated Lua debugger with an inspection facility. Together with the built-in profiling tools it is a powerful tool that can help finding the cause of bugs in your game logic or help analyze performance issues.
The simplest way to debug your game in Defold is to use print debugging. Use print()
or pprint()
statements to watch variables or indicate the flow of execution. If a game object without a script acts weird, you can just attach a script to it with the sole purpose of debugging. Using any of the printing functions will print to the Console view in the editor and to the game log.
In addition to printing, the engine can also draw debug text and straight lines on the screen. This is done by posting messages to the @render
socket:
-- Draw value of "my_val" with debug text on the screen
msg.post("@render:", "draw_text", { text = "My value: " .. my_val, position = vmath.vector3(200, 200, 0) })
-- Draw colored text on the screen
local color_green = vmath.vector4(0, 1, 0, 1)
msg.post("@render:", "draw_debug_text", { text = "Custom color", position = vmath.vector3(200, 180, 0), color = color_green })
-- Draw debug line between player and enemy on the screen
local start_p = go.get_position("player")
local end_p = go.get_position("enemy")
local color_red = vmath.vector4(1, 0, 0, 1)
msg.post("@render:", "draw_line", { start_point = start_p, end_point = end_p, color = color_red })
The visual debug messages adds data to the rendering pipeline and is drawn as part of the regular render pipeline.
"draw_line"
adds data that is rendered with the render.draw_debug3d()
function in the render script."draw_text"
is rendered with the /builtins/fonts/debug/always_on_top.font
that uses the /builtins/fonts/debug/always_on_top_font.material
material."draw_debug_text"
is the same as "draw_text"
, but it’s rendered in a custom color.Note that you probably want to update this data every frame so posting the messages in the update()
function is a good idea.
To run the debugger, either Debug ▸ Run with Debugger which starts up the game with the debugger attached, or select Debug ▸ Attach Debugger to attach the debugger to an already running game.
As soon as the debugger is attached, you have control of the execution of the game through the debugger control buttons in the console, or through the Debug menu:
end
statement below the line with the call to the function nextspawn()
:
A line of Lua code does not correspond to a single expression. Stepping in the debugger moves ahead one expression at a time, meaning that currently you may have to hit the step button more than once to advance to the next line.
nextspawn()
:
To set or clear a breakpoint, click in the column just right of the line numbers in the code editor. You can also select Edit ▸ Toggle Breakpoint from the menu.
To edit the breakpoint condition, right-click in the column just right of the line numbers in the code editor, or select Edit ▸ Edit Breakpoint from the menu.
It is currently not possible to modify variables through the evaluator.
Lua comes with a debug library that is useful in some situations, particularly if you need to inspect the innards of your Lua environment. You can find more information about it here: http://www.lua.org/pil/contents.html#23.
If you encounter an error or if your game does not behave like expected, here is a debugging checklist:
Check the console output and verify that there are no runtime errors.
Add print
statements to your code to verify that the code is actually running.
If it’s not running, check that you have done the proper setup in the editor required for the code to run. Is the script added to the right game object? Have your script acquired input focus? Are the input-triggers correct? Is the shader code added to the material? Etc.
If your code is depending on the values of variables (in an if-statement, for example), either print
those values where they are used or checked, or inspect them with the debugger.
Sometimes finding a bug can be a hard and time consuming process, requiring you to go through your code bit by bit, checking everything and narrowing down the faulty code and eliminating sources of error. This is best done by a method called “divide and conquer”:
Happy hunting!
If you have problems with physics and collisions aren’t working as expected it is recommended to enable physics debugging. Check the Debug checkbox in the Physics section of the game.project file:
When this checkbox is enabled Defold will draw all collision shapes and contact points of collisions:
Did you spot an error or do you have a suggestion? Please let us know on GitHub!
GITHUB