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

API Help

Started by grand_mind1, 24 March 2013 - 03:02 PM
grand_mind1 #1
Posted 24 March 2013 - 04:02 PM
I'm trying to make my first API. Right now it just has a few functions for how I like to do my turtle quarrys and I'm trying to get it to work. I know that all of my functions are working and I know that I have the right name for the function but when I try to load the API onto a different program and attempt to use a function, it gives me the error:

quarry13: attempt to call nill
API Code:

local function numbItemsInInv()
  local items = 0
  for i = 1, 16 do
	items = items + turtle.getItemCount(i)
  end
  return items
end

local function forward(forw)
  if tonumber(forw) == nil then
	forw = 1
  end
  for i = 1,forw do
	while not turtle.forward() do
	  turtle.dig()
	  turtle.attack()
	end
  end
end

local function up(up)
  if tonumber(up) == nil then
	up = 1
  end
  for i = 1,tonumber(up) do
	while not turtle.up() do
	  turtle.digUp()
	  turtle.attackUp()
	end
  end
end

local function empty()
  turtle.select(16)
  up()
  turtle.down()
  turtle.placeUp()
  items = numbItemsInInv()
  while items ~= 0 do
	print("Try to empty the turtle's inventory")
	for i = 1,16 do
	  turtle.select(i)
	  turtle.dropUp()
	end
	items = numbItemsInInv()
	if items ~= 0 then
	  print("Failed to empty inventory")
	  sleep(5)
	end
  end
  print("Inventory is empty!")
  sleep(8)
  turtle.select(1)
  turtle.digUp()
  turtle.transferTo(16)
end

local function inv()
  if turtle.getItemCount(15) > 0 then
	empty()
  end
  return
end

local function refuel()
  fuel = turtle.getFuelLevel()
  while fuel < 300 do
	print("I don't have enough fuel!")
	turtle.refuel()
	sleep(2)
  end  
  print("I got enough fuel!")
end

local function columnd(depth)
  refuel()
  for i = 1,tonumber(depth) do
	turtle.digDown()
	turtle.down()
	for i = 1,4 do
	  turtle.dig()
	  turtle.turnLeft()
	end  
	inv()
  end
end

local function columna(depth)
  refuel()
  for i = 1,tonumber(depth) do
	for i = 1,4 do
	  turtle.dig()
	  turtle.turnLeft()
	end
	inv()
	turtle.digUp()
	turtle.up()
  end
end

local function clean(depth)
  for i = 1,tonumber(depth) do
	turtle.digDown()
	turtle.down()
	turtle.dig()
	inv()
  end
  turtle.turnRight()
  forward(1)
  turtle.turnLeft()
  for i = 1,tonumber(depth) do
	turtle.dig()
	up(1)
	inv()
  end
  empty()
end

local function four(four)
  columnd(four)
  empty()
  forward(3)
  columna(four)
  empty()
  turtle.turnLeft()
  forward(3)
  turtle.turnRight()
  columnd(four)
  empty()
  turtle.turnLeft()
  turtle.turnLeft()
  forward(3)
  turtle.turnRight()
  turtle.turnRight()
  columna(four)
  empty()
  forward(1)
  turtle.turnRight()
  forward(1)
  turtle.turnLeft()
  clean(four)
  print("I'm done!")
end
Program where I try to use the API:

os.loadAPI("qry")
local tArgs = {...}
if ... == "clean" then
  qry.clean() --The error also occurs on this line, so I'm pretty sure it is not a problem with the API
elseif ... == nil then
  print("Am I in the bottom right hand corner?")
  print("Y/N")
  event, key = os.pullEvent("key")
  if key == 21 then
	print("How far down?")
	local four = read() --The variable 'four' is used in the function to determine how far down it should go
	print("Starting!")
	qry.four(four) --Where the error occurs
  elseif key == 49 then
	print("Well put me there!")
	sleep(2)
	os.reboot()
  end
end
I've looked up tutorials on how to use and load API's and I thought I was doing everything right.
If someone could tell me what I am doing wrong that would be greatly appreciated!
Thanks! :D/>
GravityScore #2
Posted 24 March 2013 - 07:14 PM
In your API, you define the function "four" as local. This means that it can only be used inside your API, not outside of it by any programs that load it by os.loadAPI. If you want to use a function outside of your API, you just remove the local, so the function becomes global.

Change:

local function four(four)

To:

function four(four)
remiX #3
Posted 24 March 2013 - 07:52 PM
All the functions in the API that you will be using in other programs must NOT be declared as local.
As gravityscore said, If you do this you won't be able to use the functions
grand_mind1 #4
Posted 24 March 2013 - 08:28 PM
Oh, cool. I think I remember reading about that somewhere. I just got in the habit of putting local in front of everything!
Thanks for the help!