如何使用 VSCode 调试 Lua Love2D?

Sew*_*cca 7 lua love2d visual-studio-code

我正在寻找有关如何在Visual Studio Code中调试 Lua Code 的建议。我正在使用Love2D,所以我知道我需要以某种方式嵌入我的调试代码,因为它不是独立的 Lua,但是我更喜欢对我的源代码进行最小程度的增强。

目标:在 VSCode 中使用断点、捕获错误和变量检查进行定期调试。我不介意使用哪个扩展,只要我可以轻松调试我的代码即可。

到目前为止我尝试过的:

  1. Lua 调试器:它以某种方式工作,它遇到了断点,但只有在调用时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
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。

Sew*_*cca 11

几秒钟后,我确实偶然发现了一个可行的解决方案。

安装:本地Lua调试器

将其添加到您的launch.json

    [
        {
            "type": "lua-local",
            "request": "launch",
            "name": "Debug Love",
            "program": {
                "command": "/usr/bin/love"
            },
            "args": [ "${workspaceFolder} "]
        }
    ]
Run Code Online (Sandbox Code Playgroud)

放:

if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then
    require("lldebugger").start()
end
Run Code Online (Sandbox Code Playgroud)

在你的文件的顶部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
Run Code Online (Sandbox Code Playgroud)

享受调试的乐趣!