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

[Question]-[Lua]-[help]-[ComputerCraft]How to auto use fuel in a turtle?

Started by harlekintiger, 10 December 2012 - 06:19 AM
harlekintiger #1
Posted 10 December 2012 - 07:19 AM
Hallo everyone,
I have the problem, that my turtle stops running my programm, when the fuel is over!
(I mean, I said in lua "turtle.refuel()" because that was the only way to get this thing to move…
And if this peace of coal is over, the turtle stops moving!)
I want that the turtle automatically use the coal which is still in slot 1 (the selected one!)

I never had this problem if I run for example the "tunnel" programm which was already there!
If I place a new turtle, it won't move (with "turtle.forward()" manuelly or in my programms!) untill I write "turtle.refuel()"!
BUT I don't need to do that if I use the "tunnel" programm! It will just use up the coal in the slot without any trouble!

So my question is:
Do you need a refuel command in the programm? I didn't found that in any tutorials (I just found outdated tutorials for everything!
Even from the wiki's tutorials nothing works in the new Version…) I looked in the tunnel code but didn't unterstand anything! ;D
I'm new in the lua - or programming at all! (Started watching videos about ComputerCraft programming yesterday)
(Just "quickly" wrote a programm for a branchmine (editable) and then the programm for a room digger (-this is where I have the trouble)

I'll post the code here - maybe it's another mistake from me…
(NOTE: All red text is NOT in the turtle programm! It's just a note for you!)


print("Wie lange?: ")	    --this is german for "How long?: "
x = io.read()
x = x-1
print("Wie breit?: ")	  --this is german for "How wide?: "
print("Achtung!Nur gerade Zahlen!")	 --this is german for " Watch out! Just aliquot Numbers!" (because y = y/2)
y = io.read()
y = y+0
y = y/2

for a=1,y do
for b=1,x do
  turtle.dig()
  turtle.digUp()
  turtle.forward()
  end
turtle.digUp()
turtle.turnRight()
turtle.dig()
turtle.forward()
turtle.turnRight()
for c=1,x do
  turtle.dig()
  turtle.digUp()
  turtle.forward()
  end
turtle.digUp()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
end

(Don't know why the forum do not showing the blanks at the beginning of the line…
I know, with funktions you can make it less text! But I was happy to made it working! ;D )


Please help me,
this turtle would realy help me out if the coal problem is solved!

~harlekintiger



PS: Sorry, but how can you make this cool and useful background for code here in a forum?
Edited by
Sammich Lord #2
Posted 10 December 2012 - 08:30 AM
Use ["CODE"] ["/CODE"] tags without the "'s.
A Dork Of Pork #3
Posted 10 December 2012 - 09:17 AM
Add something like this


turtle.select(1)
local x=1
while turtle.getFuelLeve;() <= 5 &amp;&amp; x<=16 then
   turtle.refuel(64)
   turtle.select(x)
   x=x+1
end
if turtle.getFuelLevel() <= 5 then
   print("Fuel Levels low! Fuel Level: "..tostring(turtle.getFuelLevel()))
end


Not tested, but should work if you put it into your for loops.
snoble2022 #4
Posted 10 December 2012 - 09:41 AM
Try this:

function AutoRefuel(slot)
  turtle.select(slot)
  turtle.refuel(64)
end


while true do
  if turtle.getFuelLevel() < 20 then
	AutoRefuel(1)
  end
  -- The rest of your program. Make sure to periodically check the level
end

harlekintiger #5
Posted 11 December 2012 - 07:20 AM
Add something like this


turtle.select(1)
local x=1
while turtle.getFuelLeve;() <= 5 &amp;&amp; x<=16 then
   turtle.refuel(64)
   turtle.select(x)
   x=x+1
end
if turtle.getFuelLevel() <= 5 then
   print("Fuel Levels low! Fuel Level: "..tostring(turtle.getFuelLevel()))
end


Not tested, but should work if you put it into your for loops.
Try this:

function AutoRefuel(slot)
  turtle.select(slot)
  turtle.refuel(64)
end


while true do
  if turtle.getFuelLevel() < 20 then
	AutoRefuel(1)
  end
  -- The rest of your program. Make sure to periodically check the level
end


I didn't understand anything ;D
I don't want to just copy-paste things - I want to understant it!
What's happening in the code you guys posted?
And where did you gor the info?

But I will try it - thanks for (trying??) helping me out! ;D
sjkeegs #6
Posted 11 December 2012 - 08:11 AM
Add something like this


while turtle.getFuelLeve;() <= 5 &amp;&amp; x<=16 then

You mis-typed getFuelLevel with a ;
Bubba #7
Posted 11 December 2012 - 03:35 PM
Added comments to their code for you.

Try this:

function AutoRefuel(slot) --Just refuels the turtle from the given slot
  turtle.select(slot)
  turtle.refuel(64)
end


while true do -- An infinite loop (unless you put a 'break' somewhere in the rest of your program)
  if turtle.getFuelLevel() < 20 then --If the turtle's fuel level is lower then 20 then refuel' it from slot 1
	AutoRefuel(1)
  end
  -- The rest of your program. Make sure to periodically check the level
end

Add something like this


turtle.select(1) --Select the first slot of the turtle
local x=0 --Number of times the turtle has refueled
while turtle.getFuelLevel() <= 5 and x<=16 then --A few errors on this line which I fixed but it checks to see if the fuel level is less than 5 and if the turtle has refueled less than 16 times and if so then it refuels until the level is either above 5 or the turtle has refueled 16 times.
   turtle.refuel(64)
   turtle.select(x)
   x=x+1 --Adds to the number of times the turtle has refueled
end
if turtle.getFuelLevel() <= 5 then --If the fuel level is less than five then it prints this message.
   print("Fuel Levels low! Fuel Level: "..tostring(turtle.getFuelLevel()))
end


Not tested, but should work if you put it into your for loops.
harlekintiger #8
Posted 13 December 2012 - 02:57 AM
Thanks @Bubba ,
this was realy helpful! Now I understand most of the stuff ;D
I'll test it later today!

But another question:
Where did all the people get the commands? I just find a few on the wiki or in tutortials…
I never found important things like the " <= 5 " thing! I would realy like to know where I get these commands…
Bubba #9
Posted 13 December 2012 - 03:39 AM
Thanks @Bubba ,
this was realy helpful! Now I understand most of the stuff ;D
I'll test it later today!

But another question:
Where did all the people get the commands? I just find a few on the wiki or in tutortials…
I never found important things like the " <= 5 " thing! I would realy like to know where I get these commands…

The best resource for ComputerCraft specific commands is probably the wiki, but it takes some searching. For example this question would only be answered (as far as I'm aware at least) by searching for "peripherals" and then scrolling to the bottom of the page where turtle commands are listed. The best resource for Lua commands is definitely the lua PIL though. Check it out here. Another way to learn is to look the code of programs in the program section. Just make sure that the programmer seems to know what they're doing though or else you'll probably end up learning bad habits from bad code :)/>/>
snoble2022 #10
Posted 31 December 2012 - 09:16 AM

if 5 >= x then

"5 >= x" is a basic math operator. It just means; if 5 is greater then or equal to (>= = greater then equal to <= = less then or equal to < = less then > greater then) x then do whats after the 'then'
Ki11er_wolf #11
Posted 31 January 2013 - 09:03 PM
If you are trying to propperly learn the best way would be to download the cc programs scripes nd tipe them in and play arround its the only way u really learn nd when looking at it don't read it like english read it like its a bunch of random words nd make it out as what the words are it makes it easier to understand. I have really only been working with lua for day nd I understand all of it nd if u are trying to learn programing I sugest u look on google for a program called scratch its almost like. A softwear developing program with minimal typeing it basicly teaches u to think in programing, it has a few commands almost but putting them togeather u can make games, movies but u can only do it in the program its how I learnt nd made java easier for me to learn as with lua, sorry if it wasn't an answer of anything u were looking for I jst though u might wanna know this.(Sorry for horribe spelling)
Doyle3694 #12
Posted 31 January 2013 - 10:53 PM
What are your goals with y = y+0?
theoriginalbit #13
Posted 31 January 2013 - 10:58 PM
-snip-
-snip-
*insert meme* Y u bump old topic? *end insert*
Seriously though this topic was last active 31 days ago…
Doyle3694 #14
Posted 31 January 2013 - 11:10 PM
haha I failed hardly, didn't look at the dates, just assumed it was active since it was last posted in not so long ago :P/>

Fail of the day from my part - anyone?
BTW, don't actually answer, then we have revived an old thread even more :S
NeverCast #15
Posted 01 February 2013 - 08:47 AM
If you are trying to propperly learn the best way would be to download the cc programs scripes nd tipe them in and play arround its the only way u really learn nd when looking at it don't read it like english read it like its a bunch of random words nd make it out as what the words are it makes it easier to understand. I have really only been working with lua for day nd I understand all of it nd if u are trying to learn programing I sugest u look on google for a program called scratch its almost like. A softwear developing program with minimal typeing it basicly teaches u to think in programing, it has a few commands almost but putting them togeather u can make games, movies but u can only do it in the program its how I learnt nd made java easier for me to learn as with lua, sorry if it wasn't an answer of anything u were looking for I jst though u might wanna know this.(Sorry for horribe spelling)

Stop tooting your own trumpet kid, no one thinks it's cool.
A DAY and you know all of Lua? bitch please!

There are people on this Forum that DO know all of Lua, so don't talk yourself up so much. Especially with spelling as good as that.
Being modest goes a long way, don't claim you know everything, prove it, and consider this is your first post. I can be sure you've not proven anything yet.

Help people is what we do here at CC, but you are not helping this guy, all you're doing is telling him how great you are and how he should Google it. If you can't offer some actual help, don't even bother typing a comment.

TheOriginalBit is right, this is an old topic. So your grave digging is not appreciated either.
Let this thread be closed!