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

Unlimited Fuel

Started by grand_mind1, 10 August 2013 - 02:55 AM
grand_mind1 #1
Posted 10 August 2013 - 04:55 AM
I'm making a refuel function to refuel turtles when needed. I know that you can change the fuel level to unlimited so that you don't have to refuel your turtles. When I set the fuel level to unlimited, it causes problems for my reufel function. My question is how can my program know that fuel is unlimited so it won't try to refuel itself?
Help is appreciated!
Thanks! :D/>
mz2212 #2
Posted 10 August 2013 - 05:06 AM
You would want to make this be the function

function refuel(fuelslot)
  if turtle.getFuelLevel() == "unlimited" then
	sleep(0.1)
  elseif turtle.getFuelLevel() <= 20
	turtle.select(fuelslot)
	turtle.refuel(1)
  else
	sleep(0.1)
  end
end
Example usage
refuel(1) – The number is the slot the fuel is in
Edited by
grand_mind1 #3
Posted 10 August 2013 - 05:11 AM
Wouldn't that check if the fuel level is equal to the variable named unlimited which doesn't exist?

[This is not correct, see immibis' post below for clarification. -L]
TheOddByte #4
Posted 10 August 2013 - 05:25 AM
Wouldn't that check if the fuel level is equal to the variable named unlimited which doesn't exist?
Yes I think so, I'm not sure if there is a turtle function or something that returns if you have unlimited fuel or something..
But I think the 'turtle.refuel()' function returns a boolean value so that might be useful.

for i = 1,16 do
 turtle.select(i)
 fuel = turtle.refuel()

if fuel then
 -- Code
else
-- Code
  end
end
Even though I don't mess around with turtles that much I hope this worked ;)/>
Otherwise check out the wiki about turtles: http://computercraft.info/wiki/Turtle.refuel
grand_mind1 #5
Posted 10 August 2013 - 03:44 PM
By researching I found out that you can use

if turtle.getFuelLevel == "unlimited" then
	--code
end
So now I have a problem. If I try to use

if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > 10 then
	--code
end
and the fuel level is unlimited then it will error, saying that I'm trying to compare a string with a number(which I am if the fuel level is unlimited). However if the fuel level is not unlimited then it will error when I try to compare the fuel level with "unlimited". How do I fix this?
CometWolf #6
Posted 11 August 2013 - 06:49 AM
the problem arises when you try to compare "unlimited" to 10. You'll have to split the code up a bit and use the type function to handle it.

local fuel = turtle.getFuelLevel()
if type(fuel) == "string" then
  -- unlimited fuel code
elseif fuel > 10 then
  -- refuel code
end
immibis #7
Posted 11 August 2013 - 08:13 AM
Wouldn't that check if the fuel level is equal to the variable named unlimited which doesn't exist?
No.

turtle.getFuelLevel() == unlimited -- this compares the fuel level to the contents of the variable "unlimited"
turtle.getFuelLevel() == "unlimited" -- this compares the fuel level to the string "unlimited".

By researching I found out that you can use

if turtle.getFuelLevel == "unlimited" then
	--code
end
So now I have a problem. If I try to use

if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > 10 then
	--code
end
and the fuel level is unlimited then it will error, saying that I'm trying to compare a string with a number(which I am if the fuel level is unlimited). However if the fuel level is not unlimited then it will error when I try to compare the fuel level with "unlimited". How do I fix this?
For one thing, you need the brackets after turtle.getFuelLevel.

turtle.getFuelLevel == "unlimited" -- this is always false unless things are seriously screwed up, because turtle.getFuelLevel is a function, not as tring.
turtle.getFuelLevel() == "unlimited" -- this is what you want

Lua will have no problem comparing a number to a string - it'll just tell you they aren't equal, like you'd expect.

123 == "unlimited" -- this is false, it doesn't raise an error

Also, with "or", if the left side is true then it won't bother running the right side:

123 < "hello" -- this raises an error
1+1 == 2 or 123 < "hello" -- this does not, since Lua stops before the second part
turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > 10 -- so this is fine too
CometWolf #8
Posted 11 August 2013 - 09:25 AM
oh shit, you're right haha. Can't believe i missed that. Now that i think about it, i've never had to use the type function myself…