citizen:/scripting/lua/scheduler.lua:61: 尝试调用一个空值 (upvalue 'fn')

所以这个代码错误就是这样的: 此外,这段lua代码用于FiveM编码,使用vRP作为主要框架。 该错误引用了一个在vRP中的函数,调用者是来自工件的基础功能。

即使如此,这是触发错误的工件代码

Code

错误的外观

local GetGameTimer = GetGameTimer
local _sbs = Citizen.SubmitBoundaryStart
local coresume, costatus = coroutine.resume, coroutine.status
local debug = debug
local coroutine_close = coroutine.close or (function(c) end) -- 5.3 compatibility
local hadThread = false
local curTime = 0

-- 设置msgpack compat
msgpack.set_string('string_compat')
msgpack.set_integer('unsigned')
msgpack.set_array('without_hole')
msgpack.setoption('empty_table_as_array', true)

-- 设置json compat
json.version = json._VERSION -- Version compatibility
json.setoption("empty_table_as_array", true)
json.setoption('with_hole', true)

-- 临时
local _in = Citizen.InvokeNative

local function FormatStackTrace()
    return _in(`FORMAT_STACK_TRACE` & 0xFFFFFFFF, nil, 0, Citizen.ResultAsString())
end

local function ProfilerEnterScope(scopeName)
    return _in(`PROFILER_ENTER_SCOPE` & 0xFFFFFFFF, scopeName)
end

local function ProfilerExitScope()
    return _in(`PROFILER_EXIT_SCOPE` & 0xFFFFFFFF)
end

local newThreads = {}
local threads = setmetatable({}, {
    -- 这绕过了"next"(因此绕过"pairs")中的未定义行为
    __newindex = newThreads,
    -- 这对于CreateThreadNow的正确工作是必需的
    __index = newThreads
})

local boundaryIdx = 1
local runningThread

local function dummyUseBoundary(idx)
    return nil
end

local function getBoundaryFunc(bfn, bid)
    return function(fn, ...)
        local boundary = bid or (boundaryIdx + 1)
        boundaryIdx = boundaryIdx + 1

        bfn(boundary, coroutine.running())

        local wrap = function(...)
            dummyUseBoundary(boundary)

            local v = table.pack(fn(...))
            return table.unpack(v)
        end

        local v = table.pack(wrap(...))

        bfn(boundary, nil)

        return table.unpack(v)
    end
end
点赞
用户2858170
用户2858170

以下是翻译结果,同时保留原本的 markdown 格式:

你的代码截图显示调用了两次 getBoundaryFunc 函数:

runWithBoundaryStart = getBoundaryFunc(Citizen.SubmitBoundaryStart)
runWithBoundaryEnd = getBoundaryFunc(Citizen.SubmitBoundaryEnd)

为了使 fn 变为 nil,这两个函数中的任意一个被调用时都必须没有提供第一个参数。

  1. 找出是否有更多对 getBoundaryFunc 函数的调用。
  2. 找出是否将返回值用 nil 替换了作为第一个参数预期的函数值。
  3. 进行修复。
2021-03-29 14:43:34