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

Refuel function

Started by grand_mind1, 17 January 2013 - 01:56 PM
grand_mind1 #1
Posted 17 January 2013 - 02:56 PM
I am trying to write a simple refuel function to call in some of my turtle programs to make sure my turtles don't run out of fuel or start without enough fuel. My code looks perfectly fine to me and when I run it I don't get any errors, but it just does nothing. I run the program and nothing happens. Would someone mind telling me what I've done wrong and what I can do to fix it? This is probably some simple mistake on my part since I am very much a beginner with Lua and computercraft. I appreciate all help and thank you in advance!
Thanks! :D/>

function checkFuel()
  m = turtle.getFuelLevel()
  n = 100000 - m
  if m < 100000 then
    print("I need ", n ," more fuel")
    print("Waiting for Fuel...")
    repeat
	  turtle.refuel()
    until m > 100000
    print("Got my fuel!")
  else
    print("I don't need fuel!")
  end
end
theoriginalbit #2
Posted 17 January 2013 - 03:01 PM
Ok so your problems:
  1. you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
  2. turtle.refuel only works on the selected slot, you need to check them all with a loop

local function checkFuel()
  while turtle.getFuelLevel() < 100000 do
	print( "I need "..( 100000 - turtle.getFuelLevel() ).." more fuel" )
	print( "Checking for fuel..." )
	for i = 1, 16 do
	  turtle.select( i )
	  turtle.refuel()
	end
	print( "Checked for fuel..."
  end
  print( "I have got enough fuel...")
end
Edited on 18 January 2013 - 01:22 AM
grand_mind1 #3
Posted 17 January 2013 - 03:14 PM
Ok so your problems:
  1. you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
  2. turtle.refuel only works on the selected slot, you need to check them all with a loop

local function checkFuel()
  while turtle.getFuelLevel() < 100000 do
	print( "I need "..( 100000 - turtle.getFuelLeve() ).." more fuel" )
	print( "Checking for fuel..." )
	for i = 1, 16 do
	  turtle.select( i )
	  turtle.refuel()
	end
	print( "Checked for fuel..."
  end
  print( "I have got enough fuel...")
end
While I do thank you for the help this still has the same problem. When I run the program nothing happens.
theoriginalbit #4
Posted 17 January 2013 - 03:21 PM
you have to call the function with this


checkFuel()
grand_mind1 #5
Posted 17 January 2013 - 03:26 PM
you have to call the function with this


checkFuel()
I'm soooooo dumb……
Dlcruz129 #6
Posted 17 January 2013 - 03:48 PM
you have to call the function with this


checkFuel()
I'm soooooo dumb……

Don't worry, you'll catch on. It can be tricky learning a new programming language.
remiX #7
Posted 18 January 2013 - 02:21 AM
Ok so your problems:
  1. you were never rechecking the fuel, so really even if it hit the right level it wouldn't have known
  2. turtle.refuel only works on the selected slot, you need to check them all with a loop

local function checkFuel()
  while turtle.getFuelLevel() < 100000 do
	print( "I need "..( 100000 - turtle.getFuelLeve() ).." more fuel" )
	print( "Checking for fuel..." )
	for i = 1, 16 do
	  turtle.select( i )
	  turtle.refuel()
	end
	print( "Checked for fuel..."
  end
  print( "I have got enough fuel...")
end

Does selecting or refueling yield?
theoriginalbit #8
Posted 18 January 2013 - 02:23 AM
Does selecting or refueling yield?
I'm not actually too sure about that… although I'm sure if it didn't we would have known since 11 hours have passed…
Cloudy #9
Posted 18 January 2013 - 02:31 AM
Selecting does.
remiX #10
Posted 18 January 2013 - 02:32 AM
Does selecting or refueling yield?
I'm not actually too sure about that… although I'm sure if it didn't we would have known since 11 hours have passed…

Or his turtle had over 100000 fuel ever since ;)/>
theoriginalbit #11
Posted 18 January 2013 - 02:35 AM
And there we go… we now know ;)/>

Cloudy is there a list compiled somewhere that contains all the CC API function calls that yield?
ChunLing #12
Posted 18 January 2013 - 12:46 PM
It's easier to list the ones that don't. If you look in the turtle API Lua code, only the functions that have been reverted to match the native versions will fail to yield. All the other's are automatically replaced with versions that yield until the native function sends back an event.

turtle["getItemCount"] = native.getItemCount
turtle["getItemSpace"] = native.getItemSpace
turtle["getFuelLevel"] = native.getFuelLevel
Edited on 18 January 2013 - 11:50 AM