turtle.refuel

From ComputerCraft
Jump to navigation Jump to search

turtle.refuel
Function
Syntax
turtle.refuel()
Returns boolean
API turtle

This function attempts to refuel the turtle by consuming amount items from the currently selected slot. If no argument is supplied, it defaults to consuming the whole stack in the slot selected.

ExampleExampleChecking if an Item is Fuel
This function simply checks if an item in the turtle's inventory at the currently selected position can be used as fuel. Since turtle.refuel takes a single argument (which is a number), you can put 0 as it's parameter to check if an item can be used as fuel.
Code
function checkIfFuel()
  return turtle.refuel(0)
end
ExampleExampleAutomatic Refueling
Checks when your turtle is low on fuel, then refuels it automatically. This function can be run in parallel with other functions. Note that this function uses the above example's function as a function.
Code
function refuel()
  local fuelLimit = turtle.getFuelLimit()
  while true do
    if turtle.getFuelLevel() < fuelLimit / 4 then
      -- if the fuel level is less than a quarter of a tank...
      -- find an item that we can use as fuel
      for i = 1, 16 do
        -- for every slot in the inventory, do...
        turtle.select(i)
        if checkIfFuel() then
          -- if inventory[i] is a fuel, then...
          turtle.refuel()
          -- refuel by consuming the whole stack
        end
      end
    end
    os.sleep(someDelayTime)
  end
end