Lua-尝试调用全局(nil值) [英] Lua - Attempt to call global (a nil value)

查看:438
本文介绍了Lua-尝试调用全局(nil值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行此代码时,出现错误试图调用全局'forId'(nil值)"

When executing this code I get an error "attempt to call global 'forId' (a nil value)"

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定义为全局代码,但错误暗示了其他情况.您可以简单地尝试更改功能定义的顺序,以查看其是否有效.

Lua processes and executes files line by line so the order you define them and use them can be important. In this case though it appears you aren't providing all the code because it looks like you are defining forID as a global but the error implies otherwise. You can simply try altering the order the functions are defined in to see if it works.

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

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

But since you haven't provided the full code this might just cause the error to shift elsewhere.

这篇关于Lua-尝试调用全局(nil值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆