开发者

Lua - Attempt to call global (a nil value)

开发者 https://www.devze.com 2023-02-25 05:05 出处:网络
When executing this code I get an error \"attempt to call global \'forId\' (a nil value)” fun开发者_如何学JAVAction execute(args)

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

fun开发者_如何学JAVAction 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)

Can anyone tell me why?


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.


try caching it as a local first, especially if your using module:

local forId = forId //or _G.forId
local bone = forId(itemid)


i think, you should include a library file at first, like i.e.

dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");

( the exact command can be found in the documentaries of your software. )

0

精彩评论

暂无评论...
验证码 换一张
取 消