Posted 14 January 2013 - 09:31 AM
The API I wrote:
The program that calls it:
And this is the error message I get when I run this program:
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.
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.