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

Problem saving the return of a command to an integer variable

Started by casilleroatr, 29 June 2013 - 07:05 AM
casilleroatr #1
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).


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
Lyqyd #2
Posted 29 June 2013 - 02:31 PM
Split into new topic.

Your capitalization doesn't match. Make sure your capitalization on invCheck (or invcheck, whichever) is consistent.
casilleroatr #3
Posted 29 June 2013 - 03:44 PM
Thank you very much. I feel silly for missing that. There were a couple of other issues I noticed after but they are fixed too.