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

How to make

Started by Paullie, 03 March 2015 - 10:21 AM
Paullie #1
Posted 03 March 2015 - 11:21 AM
Hi Community

As you may or may not know I am currently making a farming turtle to plant seeds, and when they are ready to then go out and harvest them, and repeat the cycle if I so wish. one of the problems I have whilst coding is loops, what I am trying to achieve is if the turtle is low on fuel for the in-putted length and width I want the turtle to wait for fuel like so


local fuel = turtle.getFuelLevel()
local tFuel = 16

write("Farm Length: ")
local length = io.read()
write("Farm Width: ")
local width = io.read()

totalBlocks = length * width --Depending if they are numbers WHICH THEY SHOULD BE!

--Then if the total blocks is less than the fuel level it will sit and wait for fuel
if fuel >= totalBlocks then
	  --Code which will carry out if the turtle has greater than or Equal to the fuel amount
else
	  --this is where i want the turtle to sit and wait for fuel but I dont know how
print("Please Input Fuel into Slot "..tFuel)
  --then it will sit and wait until it gets fuel
end
Edited on 03 March 2015 - 10:24 AM
The_Cat #2
Posted 03 March 2015 - 12:32 PM
Hi, so the way that i think is best is a repeat…until loop so basicly it will repeat a block of code until the parameters are reached.


This is how i would construct it:

fuelSlot = 1 --#Slot to where the fuel needs to be inserted
fuelNeeded = 2000 --#Amount of fuel needed to resume what ever it was doing before.

repeat --# Start of loop
  currFuel = turtle.getFuelLevel() --#Gets the current fuel level of the turtle.
  turtle.select(fuelSlot) --#Selects the fuel slot we defined eariler
  turtle.refuel(1) --# Refuels trying to use the slot we selected (The number in the (1) determine how much of a item will will take, so if you put 1 in there it will take 1 etc... if left empty it will consume it all)
  print("Enter fuel in slot: "..fuelSlot) --#Prints to tell user what slot to put fuel in
  print("Current Fuel Level: "..currFuel " / "..fuelNeeded) --#Prints the current fuel and fuel needed
until currFuel >= fuelNeeded --#End of loop with the parameters

This loop is better than having a while loop as it checks the parameters at the end of the loop not the start.
Also you could put this in its own funcion so you can easily use where its necessary.

This link to the computer craft wiki on loops might help too.

If you have any problems just say.
Edited on 03 March 2015 - 11:33 AM
Dog #3
Posted 03 March 2015 - 05:01 PM
nm…misread The_Cat's example… :P/>
Edited on 03 March 2015 - 04:01 PM