You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua中OutputLogMessage参数错误(期望数字得nil)的含义及修复

错误解释

错误提示[string "LuaVM"]:163: bad argument #3 to 'OutputLogMessage' (number expected, got nil)的含义是:
调用OutputLogMessage函数时,第三个参数要求传入数字,但实际传入了空值nil。

问题出在OnEvent函数的这行代码:

OutputLogMessage("event = %s, arg = %d\n", event, arg)

格式符%d强制要求对应参数为数字,但部分事件(比如PROFILE_ACTIVATED、PROFILE_DEACTIVATED)触发时,arg参数为空;另外代码里的one_到twelve_这些开关变量未初始化,首次访问时也是nil,可能引发后续逻辑异常。

修复步骤

1. 修复日志输出的类型错误

把OutputLogMessage中的%d改为%s,或者用tostring()将arg转为字符串,确保无论arg是什么类型都能正常输出:

OutputLogMessage("event = %s, arg = %s\n", event, tostring(arg))

这样即使arg是nil,也会被转为字符串"nil",不会触发类型错误。

2. 初始化开关变量

在代码开头添加变量初始化,确保所有模式开关默认处于关闭状态,避免nil导致逻辑异常:

-- 初始化所有模式开关为关闭状态
local one_ = false
local two_ = false
local three_ = false
local four_ = false
local five_ = false
local six_ = false
local seven_ = false
local eight_ = false
local nine_ = false
local ten_ = false
local eleven_ = false
local twelve_ = false

3. 可选:简化mode_change函数(减少冗余代码)

原mode_change函数重复代码过多,可简化为以下形式,降低出错概率:

function mode_change(event, arg)
    if event ~= "G_PRESSED" or arg < 1 or arg > 12 then
        return
    end
    -- 先关闭所有开关
    one_ = false
    two_ = false
    three_ = false
    four_ = false
    five_ = false
    six_ = false
    seven_ = false
    eight_ = false
    nine_ = false
    ten_ = false
    eleven_ = false
    twelve_ = false
    -- 切换当前选中的开关状态
    local switch_name = string.format("%d_", arg)
    _G[switch_name] = not _G[switch_name]
end
修复后的完整代码示例
-- 初始化所有模式开关为关闭状态
local one_ = false
local two_ = false
local three_ = false
local four_ = false
local five_ = false
local six_ = false
local seven_ = false
local eight_ = false
local nine_ = false
local ten_ = false
local eleven_ = false
local twelve_ = false

function mode_change(event, arg)
    if event ~= "G_PRESSED" or arg < 1 or arg > 12 then
        return
    end
    -- 先关闭所有开关
    one_ = false
    two_ = false
    three_ = false
    four_ = false
    five_ = false
    six_ = false
    seven_ = false
    eight_ = false
    nine_ = false
    ten_ = false
    eleven_ = false
    twelve_ = false
    -- 切换当前选中的开关状态
    local switch_name = string.format("%d_", arg)
    _G[switch_name] = not _G[switch_name]
end

function OnEvent(event, arg)
    OutputLogMessage("event = %s, arg = %s\n", event, tostring(arg))
    if event == "PROFILE_ACTIVATED" then
        EnablePrimaryMouseButtonEvents(true)
    elseif event == "PROFILE_DEACTIVATED" then
        ReleaseMouseButton(2)
    end
    mode_change(event, arg)

    ---one_ SETTINGS
    if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and one_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 8)
            Sleep(10)
        until not IsMouseButtonPressed(1)
    ---two_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and two_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 5)
            Sleep(10)
        until not IsMouseButtonPressed(1)
    ---three_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and three_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 7)
            Sleep(9)
        until not IsMouseButtonPressed(1)
    ---four_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and four_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 5)
            Sleep(8)
        until not IsMouseButtonPressed(1)
    ---five_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and five_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(1, 10)
            Sleep(7)
            MoveMouseRelative(-1, 1)
            Sleep(8)
        until not IsMouseButtonPressed(1)
    ---six_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and six_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(1, 10)
            Sleep(9)
            MoveMouseRelative(-1, 1)
            Sleep(8)
        until not IsMouseButtonPressed(1)
    ---seven_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and seven_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(1, 10)
            Sleep(10)
            MoveMouseRelative(-1, 0)
            Sleep(8)
        until not IsMouseButtonPressed(1)
    ---eight_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and eight_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 6)
            Sleep(15)
        until not IsMouseButtonPressed(1)
    ---nine_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and nine_ and IsMouseButtonPressed(3) then
        repeat
            PressKey("P")
            MoveMouseRelative(0,9)
            Sleep(30)
            ReleaseKey("P")    
            Sleep(50)
        until not IsMouseButtonPressed(1)
    ---ten_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and ten_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 6)
            Sleep(9)
        until not IsMouseButtonPressed(1)
    ---eleven_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and eleven_ and IsMouseButtonPressed(3) then
        repeat
            MoveMouseRelative(0, 6)
            Sleep(11)
        until not IsMouseButtonPressed(1)
    ---twelve_ SETTINGS
    elseif event == "MOUSE_BUTTON_PRESSED" and arg == 1 and twelve_ and IsMouseButtonPressed(3) then
        return
    end
end

内容的提问来源于stack exchange,提问作者Hasan Raqib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:26:04