LUA:声明函数和数组,还是要声明变量?

我昨天遇到了一个函数和数组的问题。好像lever ID没有声明或者说什么的……

这是我的代码:

function tpp(leverID, from, to)

if item.uid == leverID and item.itemid == 1945 then

    local count_players = #to
    local store = {}

    for i = 1, count_players do
        local pid = getTopCreature(from[i]).uid
        if (pid == 0 or not isPlayer(pid)) then
                return doPlayerSendCancel(cid, 'You need ' .. count_players .. ' players to use this lever.')
        end
        store[i] = pid
    end

    for i = 1, count_players do
        doSendMagicEffect(from[i], CONST_ME_POFF)
        doTeleportThing(store[i], to[i], false)
        doSendMagicEffect(to[i], CONST_ME_TELEPORT)
    end

    doTransformItem(item.uid, item.itemid + 1)

elseif item.uid == leverID and item.itemid == 1946 then
    doTransformItem(item.uid, item.itemid -1)
end

end

function onUse(cid, item, fromPosition, itemEx, toPosition)

local pos = {
    ['pos_start'] = {
            {['x'] = 1059, ['y'] = 1034, ['z'] = 7},
            {['x'] = 1060, ['y'] = 1034, ['z'] = 7}
    },
    ['pos_end'] = {
            {['x'] = 1059, ['y'] = 1032, ['z'] = 7},
            {['x'] = 1060, ['y'] = 1032, ['z'] = 7}
    }
}

tpp(10150, pos['pos_start'], pos['pos_end'])

return true

end

我收到了这个错误:

attempt to index global 'item' (a nil value)

我是lua新手,有人可以帮我吗?谢谢!

点赞
用户5675002
用户5675002

你可能在从 onUse() 函数中调用 tpp() 函数时忘记传递 item 变量了。

由于 tpp() 函数的作用域中不存在 item 变量或其参数,该变量被视为全局变量,而且没有该名称的全局变量。

2016-05-28 11:14:23