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

[lua] [error] attempt to compare nil (trying to use variables in an API?)

Started by Sivuden, 14 October 2012 - 10:04 AM
Sivuden #1
Posted 14 October 2012 - 12:04 PM
problem solved!

Without further ado, here's the error.. (I'm fairly new to the Lua language, as well)

SpoilerThe program:

local A = (1/96)
local L = 32
local F = (A*L)
Local V = 1
minerapi.checkFuel()
minerapi.single()
minerapi.down()
turtle.turnLeft()
turtle.turnLeft()
minerapi.single()
minerapi.finished()

please note, the (variable) parts of the functions were just put in to see if I could load them that way based off a tutorial or other I read up on (google failed me…) and the error has not changed.


The API:

--API for various mining requirements!
--[[
A = area in terms of fuel/slice widthwise
F is Fuel--L is Length--x is unuseable!!
V is for the variable in that function!
(I.e. x < L in length.. in up x < v!)

REQUIRED IN PROGRAM:
A = (width/96)
L = Length
F = (A*L)
V = variable for the function (i.e. v in placement functions determines how far the turtle moves in a direction)
]]
function checkFuel(F)
turtle.select(16)							
if turtle.getItemCount(16) < F then error("Not enough fuel! Please input more fuel!!")
else turtle.refuel(F)
end
end
function single(x, L)
x = 0
while x < L do
turtle.dig()
if turtle.forward() then x = x+1
  end
end
end
function doubleUp(x, L)
x = 0
while x < L do
  turtle.dig()
   while turtle.detectUp() do
	turtle.digUp()
   end
  if turtle.forward() then x = x+1
   end
  end
end
function doubleDown(x, L)
x = 0
while x < L do
  turtle.dig()
   while turtle.detectDown() do
	turtle.digDown()
   end
  if turtle.forward() then x = x+1
   end
  end
end
function triple(x, L)
x = 0
while x < L do
  turtle.dig()
   while turtle.detectUp() do
	turtle.digUp()
   end
   while turtle.detectDown() do
	turtle.digDown()
   end
  if turtle.forward() then x = x+1
   end
  end
end
function up(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn up!")
end
while x < v do
turtle.digUp()
if turtle.up() then x = x+1
  end
end
end
function down(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn down!")
end
while x < v do
turtle.digDown()
if turtle.down() then x = x+1
  end
end
end
function goLeftsingle(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goLeftSingle!")
end
turtle.turnLeft()
while x < v do
turtle.dig()
if turtle.forward() then x = x+1
  end
end
end
function goLeftDoubleUp(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goLeftDoubleUp!")
end
turtle.turnLeft()
while x < v do
turtle.dig()
while turtle.detectUp() do
  turtle.digUp()
end
if turtle.forward() then x = x+1
  end
end
end
function goLeftDoubleDown(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goLeftDoubleDown!")
end
turtle.turnLeft()
while x < v do
turtle.dig()
while turtle.detectDown() do
  turtle.digDown()
end
if turtle.forward() then x = x+1
  end
end
end
function goLeftTriple(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goLeftTriple!")
end
turtle.turnLeft()
while x < v do
turtle.dig()
while turtle.detectUp() do
  turtle.digUp()
end
while turtle.detectDown() do
  turtle.digDown()
end
if turtle.forward() then x = x+1
  end
end
end
function goRightSingle(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goRightSingle!")
end
turtle.turnRight()
while x < v do
turtle.dig()
if turtle.forward() then x = x+1
  end
end
end
function goRightDoubleUp(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goRightDoubleUp!")
end
turtle.turnRight()
while x < v do
turtle.dig()
while turtle.detectUp() do
  turtle.digUp()
end
if turtle.forward() then x = x+1
  end
end
end
function goRightDoubleDown(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goRightDoubleDown!")
end
turtle.turnRight()
while x < v do
turtle.dig()
while turtle.detectDown() do
  turtle.digDown()
end
if turtle.forward() then x = x+1
  end
end
end
function goRightTriple(x, v)
x = 0
if v == nil then error("error: Variable 'v' not assigned when calling fctn goRightTriple!")
end
turtle.turnRight()
while x < v do
turtle.dig()
while turtle.detectUp() do
  turtle.digUp()
end
while turtle.detectDown() do
  turtle.digDown()
end
if turtle.forward() then x = x+1
  end
end
end
function finished()
shell.run('clear')
print("Finished!! Please unload and remember to refuel the device before plugging it back in!")
end


The Error:

1x1 (that's the program name)
minerapi :19: attempt to compare nil with number



addendum to prior note: whenever I have some other error in the code itself (say, lacking the second = for my comparisons) that error popped up instead. However, this error always comes back.

I'm quite sure it's related to my not being able to load the variables I set in the program itself to be used in the API functions, which is something I would very much like to know how to do. Unfortunately, I can't seem to find it- meaning it's probably so basic everyone knows… or simply can't be done.

I apologize in advance if my stupidity is offensive. :)/>/>

~Sivuden
Mads #2
Posted 14 October 2012 - 01:25 PM
Just make a function called minerapi.init(), and pass in the variables. Then you set local variables in the API equal to these arguments.
Sivuden #3
Posted 14 October 2012 - 09:04 PM
Problem solved, thank you!