This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Nar56's profile picture

attempt to compare _le on nil and number

Started by Nar56, 21 August 2015 - 03:22 PM
Nar56 #1
Posted 21 August 2015 - 05:22 PM
Hello! I've recently been trying to write a strip mining program for my turtle, but I've been running into the error stated in the title. I want the turtle to mine a distance predefined by the user. To accomplish this, I wrote the following code. Note that this isn't the full program, but specifically the error. I'll be happy to post the full program, while it may be incomplete, if necessary. (Also, I'm new at this so my code might be a little messy :mellow:/> )


function mine()
repeat
  refuel()
  stripmine2()
  stripmine2()
  stripmine2()
  stripmine2()
  torchRight()
until
  x >= tonumber(branch)
repeat
  turtle.back()
  x = x - 1
until
  x == 0
end

The functions refuel() and torchRight() are unrelated but do exactly as they say, nothing fancy there. The function stripmine2() is what does the actual mining and every time it runs it adds to x. x is the distance the turtle has gone from its original location, and the second repeat is to return it there. The error is in the line with tonumber(branch), where branch is a predefined user input that was already established using this code:


print("Designate 'branch' distance")
local branch = read()
term.clear()
print("Are these parameters correct?	   Trunk Distance: " , trunk , " Branch Distance: " , branch)

Where have I done wrong, or how can I get this working? Will post more if necessary. Thanks!
MKlegoman357 #2
Posted 21 August 2015 - 05:48 PM
My guess would be that you're inputting it a wrong number. Also, it'd be better if we could see the whole code, maybe put it on pastebin if it's long.
Edited on 21 August 2015 - 03:48 PM
Nar56 #3
Posted 21 August 2015 - 06:02 PM
That's fine. I'll post it here since it's under 200 lines.


local x = 0
local n = 0
function refuel()
  if turtle.getFuelLevel() < 26 then
    turtle.select(1)
    turtle.refuel(1)
  end
end
function torchLeft()
turtle.up()
turtle.turnLeft()
while turtle.detect() == true do
  turtle.dig()
end
turtle.select(2)
turtle.place()
turtle.turnRight()
turtle.down()
end
function torchRight()
turtle.up()
turtle.turnRight()
while turtle.detect() == true do
  turtle.dig()
end
turtle.select(2)
turtle.place()
turtle.turnLeft()
turtle.down()
end

function stripmine1()
turtle.select(3)
if turtle.detectDown() == false then
  turtle.placeDown()
end
while turtle.detect() == true do
  turtle.dig()
end
turtle.forward()
while turtle.detectUp() == true do
  turtle.digUp()
  os.sleep(0.1)
end
turtle.up()
while turtle.detectUp() == true do
  turtle.digUp()
  os.sleep(0.1)
end
turtle.up()
while turtle.detect() == true do
  turtle.dig()
  os.sleep(0.1)
end
turtle.forward()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.down()
if turtle.detectDown() == false then
  turtle.placeDown()
end
n = n + 2
end
function stripmine2()
turtle.select(3)
if turtle.detectDown() == false then
  turtle.placeDown()
end
while turtle.detect() == true do
  turtle.dig()
end
turtle.forward()
while turtle.detectUp() == true do
  turtle.digUp()
  os.sleep(0.1)
end
turtle.up()
while turtle.detectUp() == true do
  turtle.digUp()
  os.sleep(0.1)
end
turtle.up()
while turtle.detect() == true do
  turtle.dig()
  os.sleep(0.1)
end
turtle.forward()
turtle.digDown()
turtle.down()
turtle.digDown()
turtle.down()
if turtle.detectDown() == false then
  turtle.placeDown()
end
x = x + 2
end
function mine()
repeat
  refuel()
  stripmine2()
  stripmine2()
  stripmine2()
  stripmine2()
  torchRight()
until
  x >= tonumber(branch)
repeat
  turtle.back()
  x = x - 1
until
  x == 0
end
term.clear()
print("Please designate 'trunk' distance")
local trunk = read()
term.clear()
print("Designate 'branch' distance")
local branch = read()
term.clear()
print("Are these parameters correct?	   Trunk Distance: " , trunk , " Branch Distance: " , branch)
if read() == "Yes" then
print("Initiated")
else
print("Shutting Down")
os.sleep(4)
os.shutdown()
end
repeat
refuel()
stripmine1()
stripmine1()
stripmine1()
stripmine1()
torchLeft()
turtle.turnRight()
mine()
until
n >= tonumber(trunk)
Bomb Bloke #4
Posted 21 August 2015 - 11:10 PM
When you declare "mine()", "branch" hasn't been declared yet. The function therefore ends up pointing to a version of "branch" in the global namespace.

Later you declare another version of "branch", local to the script. But you never assign a value to the global version "mine()" refers to, and so attempting your comparison crashes out.

This is where you might make use of an upvalue.

http://lua-users.org/wiki/ScopeTutorial
Nar56 #5
Posted 21 August 2015 - 11:56 PM
When you declare "mine()", "branch" hasn't been declared yet. The function therefore ends up pointing to a version of "branch" in the global namespace.

Later you declare another version of "branch", local to the script. But you never assign a value to the global version "mine()" refers to, and so attempting your comparison crashes out.

This is where you might make use of an upvalue.

http://lua-users.org...i/ScopeTutorial

Thank you! I had no idea local variables were relative to their own functions! :wacko:/> I managed to get the program running perfectly by just making the variables global instead of local (since using upvalues and closures seems a little too advanced for me). It all seems to be working fine, but if I run into another issue I'll try to make use of closures. Thank you!