Programmatic Conditional Breakpoint

While breakpoints can be set on any file and can even have conditions I’ve been trying to figure out how I can conditionally break by issuing a command in the code. I hit upon an easy method.

I create a function with a condition that works like an assertion. The idea is the code will break when the condition is False. In Studio I set a breakpoint on the indicated line. Whenever this function is called the breakpoint will fire. Step Out will then allow debugging the context.

function breakpoint(condition, comment)
  if condition then return end
  --
  -- Set breakpoint here
  -- Use Step Out to continue
  --
end

This function can then be called within some code that needs debugging.

function problem()
  if condition then
    breakpoint() -- Trigger breakpoint for debugging
    ... problem code ...
  end
end

Common Module

What I’ve done is placed this in the module that contains utility functions. I require this module in almost every script I create so I only need to set the breakpoint once and then any script in the game can trigger it programatically.

-- Breakpoint module
local module = {}
function module.breakpoint(condition, comment)
  if condition then return end
  --
  -- Set breakpoint here
  -- Use Step Out to continue
  --
end
return module

Calling it looks like this.

local utility = require(path.to.utility)

-- Trigger breakpoint
utility.breakpoint()

why not just use print() to check for the condition output?

Honestly, this is how I think Roblox should have done breakpoints. Having a debug.breakpoint function which would only break in studio, so that they could be preserved with the source.

2 Likes

I do use print() a lot for debugging but the nice thing about breakpoints is that when the code is paused you can inspect the values of any of the local variables and you can step forward line by line watching as the variables change.

1 Like