table 调用函数时返回 nil 值
table returning nil value when calling function
我试图通过几个函数传递 table 并 return 它,但它只在一定程度上起作用。我几乎可以肯定它必须与范围界定有关,但我无法解决这个问题,因为我是 LUA 的新手。
我尝试将 table 放在第 1 行并将其设置为全局,但无济于事。
错误:包参数:预期 table 但没有得到。
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 手册和几个网站,但无法弄明白。
感谢您的帮助!
当 locationParam > 0 时,您的函数 mineOre 不是 return table 而是 nil。
if (locationParam > 0) then
mineOre(locationParam, movementTable)
else
return movementTable
end
因此这将导致 nil 值最终出现在 table.maxn
movement = mineOre(oreFound, movement)
returnToTunnel(movement)
我试图通过几个函数传递 table 并 return 它,但它只在一定程度上起作用。我几乎可以肯定它必须与范围界定有关,但我无法解决这个问题,因为我是 LUA 的新手。 我尝试将 table 放在第 1 行并将其设置为全局,但无济于事。 错误:包参数:预期 table 但没有得到。
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 手册和几个网站,但无法弄明白。 感谢您的帮助!
当 locationParam > 0 时,您的函数 mineOre 不是 return table 而是 nil。
if (locationParam > 0) then
mineOre(locationParam, movementTable)
else
return movementTable
end
因此这将导致 nil 值最终出现在 table.maxn
movement = mineOre(oreFound, movement)
returnToTunnel(movement)