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

turtle.getFuelLevel() confusion?

Started by Jtpetch, 25 August 2016 - 03:57 AM
Jtpetch #1
Posted 25 August 2016 - 05:57 AM
So I'm following along with Direwolf20's CC basics tutorial series, and I'm on loops.
I've got the program to make the turtle move forward the number of blocks you input, and I understand that.

So, I figured I would try to figure out how to implement a simple "Not Enough Fuel" mechanic, which would make the turtle not move, and instead print "Not enough fuel!" if the number entered is greater than it's remaining fuel level.

This is the code, and to me (a 100% noob to Lua) it seems right.

term.write("How many blocks?: ")
blocks=read()
for i=1, blocks do
  while turtle.getFuelLevel()<i do
    print("Not Enough Fuel!")
  break
  end
  while not turtle.forward() do
    turtle.dig()
  end
end

However, the problem is, it will only display "Not enough fuel" if the fuel level is 0.
I fueled it with 1 coal (80 fuel), and told it to go 100 blocks, and it happily went along.

So, what's going wrong here? Does turtle.getFuelLevel actually only return true and false? Or, am I missing something obvious? Sorry if it's something stupid :P/>

Thank you!
DevNebulae #2
Posted 25 August 2016 - 06:15 PM
From what I can see, you directly break out of the while loop when you reach it.


while turtle.getFuelLevel()<i do
    print("Not Enough Fuel!")
    break --# < This break needs to go
  end
The_Cat #3
Posted 25 August 2016 - 06:42 PM
turtle.getFuelLevel() returns and integer (Whole Number).
Pro Tip: You can always debug your program by using print statements. Such as print("Current Fuel: "..turtle.getFuelLevel())

So in the first while loop you are comparing the current amount of fuel with 'i'. 'i' is the variable which is increased in the for loop for each iteration, by this I mean 'i' is increased by value of 1 every time it starts again.
So try changing the 'i' in the first for loop to the users input. (I think thats what you want)
KingofGamesYami #4
Posted 25 August 2016 - 06:51 PM

  while turtle.getFuelLevel()<i do
    print("Not Enough Fuel!")
  break
  end

Is roughly equivalent to

  if turtle.getFuelLevel()<i then
    print("Not Enough Fuel!")
  end

Which is going to print not enough fuel once per iteration of the main loop.

However. Immediately after you check once, you decide to dig forward. I'm fairly certain this isn't the behavior you want.

I would restructure your program to do this:

1. Ask user for block number
2. Compare that to the current fuel level
3. If it's not enough, prompt the user to add fuel. Pull a turtle_inventory event until the user inserts fuel, refuel using that
4. If it's still not enough, repeat until it is
5. Start moving forward (and digging blocks in the way / attacking mobs in the way)
Jtpetch #5
Posted 26 August 2016 - 02:05 AM
Thanks so much for all the replies everyone!
*snip*
Ok, so I'm rewriting it from scratch, with that order. However, using an "if then" directly with "blocks" and turtle.getFuelLevel causes issues.
This is the current code, named "forwards2":

term.write("How many blocks?: ")
blocks=read()
print("Attempting to go "..blocks.." blocks!")
print("I have "..turtle.getFuelLevel().." fuel units!")
if turtle.getFuelLevel()~=blocks and turtle.getFuelLevel()<blocks then
   print("Not enough fuel! Add more!")
end
(Not complete yet, stopped at this issue)
However, running this, after inputting a number of blocks to go, returns the error "forwards2:5: attempt to compare string with number expected, got string"

Now, as far as I know, both the variable blocks and turtle.getFuelLevel() are giving numbers, and that's confirmed with the two print commands.
I've read over the usage for turtle.getFuelLevel(), as well as the generic "if then" statements in Lua, but I don't see what's wrong here.
It should just be able to pull the value of blocks, and compare it to the current fuel level….

…right?

(Again, sorry if it's something obvious)

EDIT: Fixed my own problem! (Sorta)
I needed to use tonumber(read()) instead of just read(), so it could compare it as an integer.
This is the code, and it works fine!
Just need to work on attempting to refuel when the player inputs fuel.

term.write("How many blocks?: ")
blocks=tonumber(read())
print("Attempting to go "..blocks.." blocks!")
print("I have "..turtle.getFuelLevel().." fuel units!")
if turtle.getFuelLevel()~=blocks and turtle.getFuelLevel()<blocks then
  print("Not enough fuel! Add more!")
else
  print("Going!")
  for i=1,blocks do
	while not turtle.forward() do
	  turtle.dig()
	end  
  end
end
Edited on 26 August 2016 - 01:08 AM