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

How to loop a program?

Started by dextermb, 08 September 2012 - 01:06 PM
dextermb #1
Posted 08 September 2012 - 03:06 PM
How to loop a program?
Lettuce #2
Posted 08 September 2012 - 03:12 PM
*whistles* Take a pick. There's infinite loops like

while true do

terminating like

for i = 1,var do
end

or wait until a condition is met like

repeat
var = var+1
until var == 8

or while something is happening like if your turtle can't go forward:

while not turtle.forward() do
turtle.dig()
end

It's more of a question of "What do I use for this program?"

Hope that helps!
dextermb #3
Posted 08 September 2012 - 03:15 PM
*whistles* Take a pick. There's infinite loops like

while true do

terminating like

for i = 1,var do
end

or wait until a condition is met like

repeat
var = var+1
until var == 8

or while something is happening like if your turtle can't go forward:

while not turtle.forward() do
turtle.dig()
end

It's more of a question of "What do I use for this program?"

Hope that helps!

So how would i loop my program?


inventory=1,16
sapling=1
bonemeal=2
wood=3
fuel=16
function chop()
turtle.dig()
turtle.digUp()
end
function moveup()
turtle.up()
end
function replant()
turtle.select(sapling) while turtle.place(sapling) do
turtle.select(bonemeal) while turtle.place(bonemeal) do
end
end
if turtle.getItemCount(sapling) <20 then
  turtle.turnRight()
  turtle.select(1)
  turtle.suck(1)
if turtle.getItemCount(bonemeal) <20 then
  turtle.select(2)
  turtle.suck(2)
  turtle.turnLeft()
end
end
end
function bank()
if turtle.getItemCount(wood) >25 then
  turtle.turnRight()
  turtle.select(wood)
  turtle.drop()
  turtle.turnLeft()
end
end
if turtle.getFuelLevel() <100 then
turtle.refuel(fuel)
end
while turtle.detect() do
print ("Chopping")
  chop()
print ("Moving Up")
  moveup()
end
print ("Moving Down")
while not turtle.detect() and not turtle.detectDown() do
  turtle.down()
end
print ("Replanting")
replant()
print ("Banking")
bank()
turtle.select(1)
Lettuce #4
Posted 08 September 2012 - 03:20 PM
You have several loops there. Do you want it to constantly run that program? Use "while true do" and put "end" at the very bottom.
dextermb #5
Posted 08 September 2012 - 03:30 PM
You have several loops there. Do you want it to constantly run that program? Use "while true do" and put "end" at the very bottom.

how would i add it to stop when i type 'stop' ?
Lettuce #6
Posted 08 September 2012 - 05:06 PM
That's parallel. I've never used it before, but if I just read it right it is:

function run()
--your whole looping code
end
function quit()
while true  do
input = io.read()
if input == "stop" then
break
end
end
parallel.waitForAll(quit, run)

As I said, I never tried it. But give it a go, it seems useful. If all else fails, hold Ctrl+T to terminate it.