moeux
moeux

Reputation: 197

table returning nil value when calling function

I'm trying to pass a table through several functions and return it, but it only works to a certain degree. I'm almost sure it has to do something with the scoping, but I can't work it out since I'm new with LUA. I tried putting the table in line 1 and setting it global, but to no avail. Error: bag argument: expected table but got nil.

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

So, the digTunnel function calls the other two functions mineOre and returnToTunnel.

I've been looking in the LUA manual and several websites but can't figure it out. Thank you for your help!

Upvotes: 1

Views: 391

Answers (1)

Piglet
Piglet

Reputation: 28974

Your function mineOre does not return a table but nil, when locationParam is > 0.

if (locationParam > 0) then
  mineOre(locationParam, movementTable)
else
  return movementTable
end

Hence this will cause a nil value end up in table.maxn

 movement = mineOre(oreFound, movement)
 returnToTunnel(movement)

Upvotes: 2

Related Questions