Lua - 尝试调用全局(nil 值)

标签 lua

执行此代码时,我收到错误消息“尝试调用全局‘forId’(一个 nil 值)”

function execute(args)
    local itemid = 526
    local bone = forId(itemid) -- this is where the error occurs
end

function forId(bid) 
    local xp = 0.0
    if bid == 526 or bid == 528 or bid == 2530 or bid == 2859 then
        xp = 4.5
    elseif bid == 3179 or bid == 3180 or bid == 3183 or bid == 3185 then
        xp = 5.0
    elseif bid == 530 then
        xp = 53
    elseif bid == 532 or bid == 3125 then
        xp = 15
    elseif bid == 4812 then
        xp = 22.5
    elseif bid == 7839 then
        xp = 30
    elseif bid == 6812 then
        xp = 50
    elseif bid == 536 then
        xp = 72
    end
    local bone = Bone:new(bid, xp)
    return bone
end

Bone = class(function(b, id, xp)
    b.id = id
    b.xp = xp
end)

谁能告诉我为什么?

最佳答案

Lua 逐行处理和执行文件,因此您定义和使用它们的顺序可能很重要。在这种情况下,虽然看起来您没有提供所有代码,因为看起来您正在定义 forID作为全局,但错误暗示否则。您可以简单地尝试更改函数定义的顺序,看看它是否有效。

Bone = class(function(b, id, xp)
    b.id = id
    b.xp = xp
end)

function forId(bid) 
    local xp = 0.0
    if bid == 526 or bid == 528 or bid == 2530 or bid == 2859 then
        xp = 4.5
    elseif bid == 3179 or bid == 3180 or bid == 3183 or bid == 3185 then
        xp = 5.0
    elseif bid == 530 then
        xp = 53
    elseif bid == 532 or bid == 3125 then
        xp = 15
    elseif bid == 4812 then
        xp = 22.5
    elseif bid == 7839 then
        xp = 30
    elseif bid == 6812 then
        xp = 50
    elseif bid == 536 then
        xp = 72
    end
    local bone = Bone:new(bid, xp)
    return bone
end

function execute(args)
    local itemid = 526
    local bone = forId(itemid) -- this is where the error occurs
end

但是由于您没有提供完整的代码,这可能只会导致错误转移到其他地方。

关于Lua - 尝试调用全局(nil 值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694331/

相关文章:

c++ - 除非复制到新字符串,否则比较从 Lua 中的 C 函数推送的字符串失败

variables - Rbx.Lua - 为什么我不能将 .txt 文件存储为表格?

multithreading - 我可以在单线程中创建多个 Lua VM 吗?

lua - 如何在 Lua 中打印连接的字符串表?

lua - 迭代表的表

inheritance - Lua 继承 - 在 super 上调用方法

c++ - Lua_hook 和 lua_Debug->event

lua - 电晕 SDK : Restart an app only when user press "home"

c# - LuaInterface - 一个将返回 LuaTable 值的函数

c# - C# 中的简单 Lua 语法检查