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

Help Fix My Refuel Function

Started by DrTickleFight, 21 January 2014 - 01:41 PM
DrTickleFight #1
Posted 21 January 2014 - 02:41 PM

fFuel = turtle.getFuelLevel()
function rFuel()
if fFuel <= 20 then
  if turtle.getItemCount(1) <= 1 then
   print("Please put fuel into slot 1 and press <ENTER>")
   pause = read()
   turtle.select(1)
   turtle.refuel(1)
   os.sleep(0.4)
  else
   turtle.sellect(1)
   turtle.refuel(1)
   os.sleep(0.4)
  end
end
end

Basicly what i want the function to do is check if the fuel level is 20 or below, and then check if there is any coal in slot 1. If there is no coal i want it to ask for the user to input coal. And if there is, i want it to run the else statement. However, when i run the code with no coal in slot 1, it asks "Please put fuel into slot 1 and press <ENTER>" I do this and it refuels successfully, but it loops back to the beginning and asks for more coal. Can anyone help me fix this or improve my code. Thanks. P.S. I'm fairly new to lua so if you could provide an explanation of your fix that would be great.
surferpup #2
Posted 21 January 2014 - 03:28 PM
There is no looping statement in your function.

It appears that you are checking to see if there is one or less items in slot 1. Your code reads:

if turtle.getItemCount(1) <= 1 then

Don't you mean to test for "less than one item" in slot 1? I changed the line to read:

if turtle.getItemCount(1) < 1 then

Also, since it appears you are just asking for the user to press any key, I suggest using the os.pullEvent("key") function over read. What the os.pullEvent("key") function will do is to simply pause the program until the user presses a key. Since you don't care about the value of the key press, there is no need to store the return variables anywhere.

You also have no need for the else statement and the repeated code block with which it is associated. I moved the following section of code to just past the end in the if control block and deleted the else block and the repeated code within. I have no opinion on whether you need to use os.sleep(0.4) in this section, so I just left it in.


turtle.select(1)
turtle.refuel(1)
os.sleep(0.4)

Try this code corrected code instead. Does it work for you? When you call rFuel(), the function will loop until the fuel level is 20 or above. If there is less than one item in slot 1, it will make a user request for an item and wait for a key press. The program will then select slot 1 and refuel one item worth of fuel (that is what the argument to the refuel(1) function does, it attempts to use up to the argument number of fuel items). If you wanted it to use all of the fuel items in slot 1, leave the argument out – just use refuel(). Next, the program will pause for 0.4 seconds (again, I am not sure this is necessary) and repeat the while loop – testing if the fuel level is still less than 20. If in this entire process the fuel level eventually becomes 20 or above, the function will exit.


function rFuel()
  while turtle.getFuelLevel() < 20 do
	if turtle.getItemCount(1) < 1 then
	  print("Please put fuel into slot 1 and press any key")
	  os.pullEvent("key")
	end
	turtle.select(1)
	turtle.refuel(1)
	os.sleep(0.4)
  end
end
Edited on 21 January 2014 - 02:52 PM
DrTickleFight #3
Posted 21 January 2014 - 03:57 PM
Thank you so much suferpup. The function works great and i really appreciate your help.
surferpup #4
Posted 21 January 2014 - 04:21 PM
You are welcome. If you would like, there are some great tutorials on this site. Take a moment to browse a few of them. The Wiki also has some excellent tutorials.

Also – if you did like my response, do me a favor and up vote it. Thanks.