Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Lua Love2D with VSCode?

I'm looking for suggestions on how to debug Lua Code in Visual Studio Code. I'm using Love2D, so I understand that I will somehow need to embed my debugging code, because it is not standalone Lua, however I would prefer a minimal augumentation of my sources.

Goal: Regular debugging with breakpoints, catch errors and variable inspection in VSCode. I don't mind which extension I use, as long as I can debug my code with ease.

What I tried so far:

  1. Lua Debugger: It worked somehow, it hit on a breakpoint, but only when calling debuggee.poll() and from there I couldn't step in or inspect further.

  2. LRDB: Seems promising, but somehow the game won't start. It just hangs until I kill it with the task manager.

Code for LRDB (generic update/draw functions not included, cause they are just for testing breakpoints):


local lrdb = require "lrdb_server"
local db_port = 21110

function love.run()
    lrdb.activate(db_port)

    if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
 
    -- We don't want the first frame's dt to include time taken by love.load.
    if love.timer then love.timer.step() end
 
    local dt = 0
    lrdb.deactivate()
    -- Main loop time.
    return function()
        lrdb.activate(db_port)
        -- Process events.
        if love.event then
            love.event.pump()
            for name, a,b,c,d,e,f in love.event.poll() do
                if name == "quit" then
                    if not love.quit or not love.quit() then
                        return a or 0
                    end
                end
                love.handlers[name](a,b,c,d,e,f)
            end
        end
 
        -- Update dt, as we'll be passing it to update
        if love.timer then dt = love.timer.step() end
 
        -- Call update and draw
        if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
 
        if love.graphics and love.graphics.isActive() then
            love.graphics.origin()
            love.graphics.clear(love.graphics.getBackgroundColor())
 
            if love.draw then love.draw() end
 
            love.graphics.present()
        end
 
        if love.timer then love.timer.sleep(0.001) end
        lrdb.deactivate()
    end
end

Any help would be appreciated.

like image 504
Sewbacca Avatar asked Jul 19 '26 19:07

Sewbacca


1 Answers

I literally just stumbled seconds later on a working solution here.

Install: Local Lua Debugger

Add this to your launch.json:

    [
        {
            "type": "lua-local",
            "request": "launch",
            "name": "Debug Love",
            "program": {
                "command": "/usr/bin/love"
            },
            "args": [ "${workspaceFolder} "]
        }
    ]

put:

if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
    require("lldebugger").start()
end

On top of your main.lua file.

If you don't overwrite love.run you can use this snippet to catch all errors, because I have found that some errors might not get catched correctly:

if os.getenv "LOCAL_LUA_DEBUGGER_VSCODE" == "1" then
    local lldebugger = require "lldebugger"
    lldebugger.start()
    local run = love.run
    function love.run(...)
        local f = lldebugger.call(run, false, ...)
        return function(...) return lldebugger.call(f, false, ...) end
    end
end

Enjoy debugging!

like image 87
Sewbacca Avatar answered Jul 21 '26 20:07

Sewbacca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!