Posted 09 February 2016 - 10:07 AM
I have made an API for my turtles to learn objects and save the object name to a file so that the can continually build a database of known object for easy comparison to later and I want to use some of these functions in another API that I am building that manages fuel. However when I call a function from my LearnAPI from within my FuelAPI I get a error FuelAPI:line20 attempt to index ? (nil). I have posted both my APIs. I have been stuck on this for several hours and nothing I do is making any difference. Also I will post my startup script in case that helps.
startup
LearnAPI
FuelAPI
startup
os.loadAPI("LocationAPI")
print("Loaded Location API")
os.loadAPI("LearnAPI")
print("Loaded LearnAPI")
os.loadAPI("FuelAPI")
print("Loaded FuelAPI")
location = LocationAPI.Setup()
learn = LearnAPI.setup()
fuel = FuelAPI.setup()
fuel.setFuelSource()
LearnAPI
--Allows Turtle to learn new objects--
function setup()
local this = {
learned_items = {}
}
local saveItems = function ()
save = fs.open("known_blocks", "w")
save.write (textutils.serialize({this.learned_items}))
save.close()
end
local loadItems = function ()
if fs.exists("known_blocks") then
load = fs.open("known_blocks", "r")
this.learned_items = unpack(textutils.unserialize(load.readAll()))
print("...Loading known items...")
load.close()
else
print("I dont know any blocks.")
end
end
local getLearnedItems = function ()
loadItems()
for i = 1, #this.learned_items do
print(this.learned_items[i])
end
end
local known = function (search)
loadItems()
for i = 1, #this.learned_items do
if search == this.learned_items[i] then
return true
end
end
end
local scan = function ()
loadItems()
local success, item = turtle.inspect()
if success then
if known(item.name) == true then
print("Item already known.")
else
table.insert(this.learned_items, item.name)
print ("Learned ", item.name)
saveItems()
end
end
end
local scanInventory = function()
loadItems()
for i = 1, 16 do
if turtle.getItemCount(i) ~= 0 then
local item = turtle.getItemDetail(i)
if known(item.name) == true then
print("Item already known.")
else
table.insert(this.learned_items, item.name)
print ("Learned ", item.name)
saveItems()
end
end
end
end
return {
scan = scan,
getLearnedItems = getLearnedItems,
scanInventory = scanInventory,
known = known
}
end
FuelAPI
function setup()
local this = {
min_fuel_level = 10,
fuel_sources = {"minecraft:coal", "minecraft:planks", "minecraft:log"},
}
local refuel = function ()
if turtle.getFuelLevel() < this.min_fuel_level then
turtle.refuel()
end
end
local setMinFuelLevel = function (min_level)
this.min_fuel_level = min_level
print("Minimum fuel level set to " , this.min_fuel_level)
end
local setFuelSource = function ()
print("in fuel")
for i = 1, #this.fuel_sources do
if learn.known(this.fuel_sources[i]) then
print("I have ", this.fuel_sources[i])
end
end
end
return {
refuel = refuel,
setMinFuelLevel = setMinFuelLevel,
setFuelSource = setFuelSource,
}
end