如何使用VSCode调试Lua Love2D?

7
我正在寻求关于如何在Visual Studio Code中调试Lua代码的建议。 我正在使用Love2D,因此我知道我需要嵌入我的调试代码,因为它不是独立的Lua,但是我希望对我的源代码进行最小化的扩展。
目标:在VSCode中使用断点进行常规调试,捕获错误并检查变量。 我不介意使用哪个扩展,只要我能轻松地调试我的代码即可。
我尝试过以下方法: 1. Lua Debugger:它在某种程度上起作用,当调用debuggee.poll()时会触发断点,但从那里我无法进一步步进或检查。 2. LRDB:看起来很有前途,但游戏无法启动。 它只是挂起,直到我用任务管理器将其关闭。

LRDB的代码(不包括通用的更新/绘制函数,因为它们只是用于测试断点):


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

任何帮助都将不胜感激。
1个回答

11

我刚刚偶然发现了一个可行的解决方案,在这里

安装:本地 Lua 调试器

将以下内容添加到您的 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

在你的main.lua文件顶部。

如果您没有覆盖love.run函数,您可以使用此代码段捕获所有错误,因为我发现有些错误可能无法正确地被捕获:

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

享受调试吧!


1
有什么想法,如果窗口只是保持空白(白色)?我已经双重检查了启动配置路径... - Manticore
没关系。我通过将 debug-lib-functionality 赋值给变量而覆盖了它的调试库功能,因此它无法工作。 - Manticore

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接