[调用函数时表返回零值

问题描述 投票:1回答:1

我正在尝试通过多个函数传递表并返回它,但是它只能在某种程度上起作用。我几乎可以肯定它必须与范围界定有关,但是由于我是LUA的新手,所以我无法解决。我尝试将表放在第1行并将其设置为全局,但无济于事。错误:bag参数:预期表,但为零。

function returnToTunnel(movementTable)
    for i = table.maxn(movementTable), 1, -1  do  --this is where I get the error.
        if (movementTable[i] == 1) then
            turtle.down()
        elseif (movementTable[i] == 2) then
            turtle.up()
        elseif (movementTable[i] == 3) then
            turtle.back()
            turtle.turnRight()
        elseif (movementTable[i] == 4) then
            turtle.back()
            turtle.turnLeft()
        elseif (movementTable[i] == 5) then
            turtle.back()
        end
    end
end

function mineOre(locationParam, movementTable)
    if (locationParam == 1) then
        turtle.digUp()
        turtle.suckUp()
        turtle.up()
        table.insert(movementTable, 1)
    elseif (locationParam == 2) then
        turtle.digDown()
        turtle.suckDown()
        turtle.down()
        table.insert(movementTable, 2)
    elseif (locationParam == 3) then
        turtle.turnLeft()
        turtle.dig()
        turtle.suck()
        turtle.forward()
        table.insert(movementTable, 3)
    elseif (locationParam == 4) then
        turtle.turnRight()
        turtle.dig()
        turtle.suck()
        turtle.forward()
        table.insert(movementTable, 4)
    elseif (locationParam == 5) then
        turtle.dig()
        turtle.suck()
        turtle.forward()
        table.insert(movementTable, 5)
    end
    locationParam = oreCheck()
    if (locationParam > 0) then
        mineOre(locationParam, movementTable)
    else
        return movementTable
    end
end

function digTunnel(tunnelLengthParam)
    local oreFound
    local movement = {}

    for i = 1, tunnelLengthParam do
        turtle.dig()
        turtle.forward()
        oreFound = oreCheck()
        if (oreFound > 0) then
            movement = mineOre(oreFound, movement)
            returnToTunnel(movement)
        end
        if ((i % 2) == 1) then
            turtle.digUp()
            turtle.up()
        elseif ((i % 2) == 0) then
            turtle.digDown()
            turtle.down()
        end
        oreFound = oreCheck()
        if (oreFound > 0) then
            movement = mineOre(oreFound, movement)
            returnToTunnel(movement)
        end
    end
end

因此,digTunnel函数调用其他两个函数mineOre和returnToTunnel。

我一直在寻找LUA手册和一些网站,但无法弄清楚。谢谢您的帮助!

function lua parameter-passing lua-table
1个回答
2
投票

当locationParam为> 0时,您的功能mineOre不返回表,而是返回nil。

© www.soinside.com 2019 - 2024. All rights reserved.