How to debug Lua Love2D with VSCode?
Asked Answered
B

1

7

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.

Benjaminbenji answered 29/11, 2020 at 22:45 Comment(0)
B
13

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!

Benjaminbenji answered 29/11, 2020 at 23:0 Comment(2)
any idea whats the problem if the window just stays blank (white)? I double-checked launch config paths...Palestine
nevermind. I overwrote the debug-lib-functionality by assigning it to a variable and therefore it did not workPalestine

© 2022 - 2024 — McMap. All rights reserved.