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

[RESOLVED] [Error] Attempt to call nil when using a function from a custom API

Started by Signatune, 14 January 2013 - 08:31 AM
Signatune #1
Posted 14 January 2013 - 09:31 AM
The API I wrote:


local function move(x, y, z)
-- X-AXIS MOVEMENT --
if x==0 then print("No x-axis movement.")
elseif x>1 then -- Moves in a positive direction (forward)
print("Moving on x-axis "..tostring(x).." units.")
for l=1,x do
  if turtle.forward() then print("Moving!")
  else turtle.dig() turtle.forward() print("Had to dig!")
  end
end
else -- Moves in a negative direction (backward)
  print("Turning around!")
  turtle.turnLeft()
  turtle.turnLeft()
  print("Moving on x-axis "..tostring(x).." units.")
  for l=1,x do
   if turtle.forward() then print("Moving!")
   else turtle.dig() turtle.forward() print("Had to dig!")
   end
  end
end
-- Y-AXIS MOVEMENT --
if y==0 then print("No y-axis movement.")
  elseif y>1 then -- Moves in a positive direction (up)
   print("Moving on y-axis "..tostring(y).." units.")
   for l=1,y do
	if turtle.up() then print("Moving!")
	else turtle.digUp() turtle.up() print("Had to dig!")
	end
   end
  else -- Moves in a negative direction (down)
   print("Moving on y-axis "..tostring(y).." units.")
   for l=1,y do
	if turtle.down() then print("Moving!")
	else turtle.digDown() turtle.down() print("Had to dig!")
	end
   end
end
-- Z-AXIS MOVEMENT --
if z==0 then print("No z-axis movement.")
  elseif z>1 then -- Moves in a positive direction (right)
   print("Turning!")
   turtle.turnRight()
   print("Moving on z-axis "..tostring(z).." units.")
   for l=1,z do
	if turtle.forward() then print("Moving!")
	else turtle.dig() turtle.forward() print("Had to dig!")
	end
   end
  else -- Moves in a negative direction (left)
   print("Turning!")
   turtle.turnLeft()
   print("Moving on z-axis "..tostring(z).." units.")
   for l=1,z do
	if turtle.forward() then print("Moving!")
	else turtle.digDown() turtle.down() print("Had to dig!")
	end
   end
end
end

The program that calls it:


os.loadAPI("rom/apis/turtle/addapi")
addapi.move(-2, 0, -5)

And this is the error message I get when I run this program:


apitest2:2: attempt to call nil

The code works when it's in its own program, but not when I make it into an API, so I think it has something to do with how API's are used. I don't have any lua experience and I've never dealt with APIs before. Thanks.
imef #2
Posted 14 January 2013 - 09:51 AM
The problem is that you declared a local function in the API.

To fix the API just replace the first line with :

function move(x, y, z)

Signatune #3
Posted 14 January 2013 - 10:01 AM
Thanks, that fixed the error! Although now, I realized that it can only move in positive directions. I can't really see why though after looking at the code.
remiX #4
Posted 14 January 2013 - 10:43 AM
Because you made only work if x, y, or z is greater than 1.
Signatune #5
Posted 14 January 2013 - 10:46 AM
I don't see how though. The elseif statement should receive false if any of the numbers are negative. Besides, it turns around fine, and the code for that is in the false part of the elseif.
Signatune #6
Posted 14 January 2013 - 10:55 AM
Nevermind, figured it out. Thanks for your help everybody!