VSCODE调试LOVE引擎游戏

安装插件

[Love] VSCODE 调试 LOVE 引擎游戏-LMLPHP[Love] VSCODE 调试 LOVE 引擎游戏-LMLPHP

配置插件

CTRL + SHIFT + P,打开Preferences: Open User Settings (JSON),为settings.json添加如下代码

  "Lua.runtime.version": "LuaJIT",
  "Lua.diagnostics.globals": [
    "love",
  ],
  "Lua.workspace.library": [
    "${3rd}/love2d/library"
  ],
  "Lua.workspace.checkThirdParty": false,

添加系统环境变量

[Love] VSCODE 调试 LOVE 引擎游戏-LMLPHP

配置启动文件

Run and Debug配置launch.json,该文件将出现在你工作目录下的 .vscode 文件夹

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lua-local",
      "request": "launch",
      "name": "Debug",
      "program": {
        "command": "love"
      },
      "args": [
        ".",
        "debug"
      ],
    },
    {
      "type": "lua-local",
      "request": "launch",
      "name": "Release",
      "program": {
        "command": "love"
      },
      "args": [
        ".",
      ],
    },
  ]
}

添加至 main.lua最顶部

if arg[2] == "debug" then
    require("lldebugger").start()
end

添加至main.lua最底部(可选)

出错信息不会显示在游戏窗口画面上,而是直接定位到当前错误的行

[Love] VSCODE 调试 LOVE 引擎游戏-LMLPHP
local love_errorhandler = love.errhand

function love.errorhandler(msg)
    if lldebugger then
        error(msg, 2)
    else
        return love_errorhandler(msg)
    end
end

关闭外部控制台

修改conf.lua,关闭t.console,否则调试器会无法获取到信息而卡住

-- conf.lua
love.conf = function(t)
    -- t.console = true
end

启动调试

按F5启动游戏,你现在可以选择两种方式来启动

  • Debug 可以调试游戏(需要设置断点)
  • Release 直接运行游戏
[Love] VSCODE 调试 LOVE 引擎游戏-LMLPHP

如果出现提示 '"love"' �����ڲ����ⲿ���Ҳ���ǿ����еij��� ���������ļ��� 只需要重启VSCODE


关于调试器

tomblind/local-lua-debugger-vscode: Local Lua Debugger for VSCode (github.com)

⭐ 快速模板

if arg[2] == "debug" then
  require("lldebugger").start()
end

-- your code

local love_errorhandler = love.errhand

function love.errorhandler(msg)
    if lldebugger then
        error(msg, 2)
    else
        return love_errorhandler(msg)
    end
end

{
  "version": "0.2.0",
  "configurations": [
    
    {
      "type": "lua-local",
      "request": "launch",
      "name": "Debug",
      "program": {
        "command": "love"
      },
      "args": [
        ".",
        "debug"
      ],
    },
    {
      "type": "lua-local",
      "request": "launch",
      "name": "Release",
      "program": {
        "command": "love"
      },
      "args": [
        ".",
      ],
    },
  ]
}
08-24 08:55