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

Turtle Does Not Wait For Loop

Started by macmoholic, 28 August 2016 - 05:08 AM
macmoholic #1
Posted 28 August 2016 - 07:08 AM
Hello, I'm brand-spankin' new on the forum.
I read the "FAQ" post and, forgive me if I'm wrong, don't see if any of it applies to my loop. That being said, I have a mining turtle program that will dig a tunnel of my preferred height over the 'tunnel' program. Within it is a "wait to refuel" function that, of course, waits for fuel to be dumped into the inventory of the turtle. It looks like so:

function fuelCheck()
  if(turtle.refuel(0)) then
	turtle.refuel(1)
  else
	print("Waiting for fuel.")
	while(turtle.getFuelLevel() < 6) do
	  for i = 1, 16 do
		turtle.select(i)
		turtle.refuel(1)
	  end
	end
  end
end
//mining functions here...

Unfortunately the problem I'm having is the turtle, when out of fuel, gets to the point where it prints "Waiting for fuel." and prints that. Then it appears to run the while loop ("while(turtle.getFuelLevel() < 6") a single time and then begins the mining functions below this function (Of course it's not able to do any movement forward so it just moves up and down doing turns and ups and downs alone). I'm guessing something is wrong with the while loop, which would make me feel a tad worthless, or there's something wrong with an infinite loop running waiting for a change in inventory.

What am I doing wrong here?
valithor #2
Posted 28 August 2016 - 05:58 PM
If it is indeed moving up and down it has fuel. Moving up and down does require fuel to actually move. You might want to change your refuel function to something like this:

function fuelCheck()
  if turtle.getFuelLevel() < 6 then
    if(turtle.refuel(0)) then
      turtle.refuel(1)
    else
      print("Waiting for fuel.")
      while(turtle.getFuelLevel() < 6) do
        for i = 1, 16 do
          turtle.select(i)
          turtle.refuel(1)
        end
      end
    end
  end
end

That extra if statement at the top makes it to where it will only try to refuel if it has less than 6 fuel, so it will only print it is waiting for fuel if it is out of fuel AND the currently selected slot contains no fuel. Chances are it is printing it is waiting for fuel, but it is above 6 fuel, which would mean the while loop would never actually run.
Edited on 28 August 2016 - 04:00 PM
macmoholic #3
Posted 28 August 2016 - 11:45 PM
Awesome, I'll try that out. Thanks so much!