Posted 29 June 2013 - 09:05 AM
Title: Problem saving the return of a command to an integer variable
I am making a turtle that will make dark iron blocks for me with Applied Energistics. Every 10 seconds the turtle will check if there is an iron block in slot 1. I used the command turtle.getItemCount(1) to do this. If the answer is 0 I want the turtle to go back to sleep, if not I want it to make dark iron.
I wrote this:
local invCheck = turtle.getItemCount(1)
hoping to get an integer between 0 and 64 saved to invCheck. However I think it is just saving a nil value or something. As a result the function where the turtle checks whether it has work to do doesn't work properly (it checks if the answer is 0 but it never is even if there aren't any items in the inventory).
I also wrote a for loop that starts at one and is meant to run long enough for every block of iron to have been turned into dark iron but I get an error saying that the for limit must be a number.
So how do I make sure that it is a number.
Thank you for reading this far. Any advice would be received gratefully.
Here is the rest of the code (hopefully there are no other big problems).
I am making a turtle that will make dark iron blocks for me with Applied Energistics. Every 10 seconds the turtle will check if there is an iron block in slot 1. I used the command turtle.getItemCount(1) to do this. If the answer is 0 I want the turtle to go back to sleep, if not I want it to make dark iron.
I wrote this:
local invCheck = turtle.getItemCount(1)
hoping to get an integer between 0 and 64 saved to invCheck. However I think it is just saving a nil value or something. As a result the function where the turtle checks whether it has work to do doesn't work properly (it checks if the answer is 0 but it never is even if there aren't any items in the inventory).
function workCheck(invCheck)
if invCheck == 0 then
noWork()
else
work()
end
end
I also wrote a for loop that starts at one and is meant to run long enough for every block of iron to have been turned into dark iron but I get an error saying that the for limit must be a number.
for i = 1, (invCheck) do
turtle.select(1)
turtle.place()
sleep(3)
turtle.select(16)
local darkIronCheck = turtle.compare()
if darkIronCheck == true then
turtle.dig()
else
waitForDarkIron(darkIronCheck)
turtle.dig()
end
end
So how do I make sure that it is a number.
Thank you for reading this far. Any advice would be received gratefully.
Here is the rest of the code (hopefully there are no other big problems).
function workCheck(invCheck)
if invCheck == 0 then
noWork()
else
work()
end
end
function noWork()
print("No work")
endfunction slotTwoClear()
turtle.select(2)
turtle.turnRight()
turtle.turnRight()
turtle.drop()
turtle.turnRight()
turtle.turnRight()
print("emptied slot 2")
end
function waitForDarkIron(darkIronCheck)
while darkIronCheck == false do
sleep(1)
darkIronCheck = turtle.compare
end
end
function work(invCheck)
print("Work")
if invCheck == 64 then
local invTwo = turtle.getItemCount(2)
if invTwo ~= 0 then
slotTwoClear()
end
end
for i = 1, (invCheck) do
turtle.select(1)
turtle.place()
sleep(3)
turtle.select(16)
local darkIronCheck = turtle.compare()
if darkIronCheck == true then
turtle.dig()
else
waitForDarkIron(darkIronCheck)
turtle.dig()
end
end
print("Dark Iron Collected")
invclear()
print("Inventory Cleared")
end
function invclear()
turtle.turnRight()
turtle.turnRight()
for i = 2, 15 do
turtle.select(i)
turtle.drop()
end
turtle.turnRight()
turtle.turnRight()
end
local invcheck
while true do
term.clear()
term.setCursorPos(1,1)
turtle.select(1)
invcheck = turtle.getItemCount(1)
workCheck(invCheck)
sleep(15)
end