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

[Lua] How to create an exitable loop on startup?

Started by NdMSnIpEz, 11 May 2013 - 04:45 PM
NdMSnIpEz #1
Posted 11 May 2013 - 06:45 PM
So, I created a program and want to run it on startup. However if I do this:

while true do
   shell.run("cell")
end
Then the turtle gets stuck in an infinite loop. Can't terminate. Have to replace the turtle.
Is there any way to bypass this but still have it start up when I load my world?
By the way, my turtle is chunkloaded by a spot loader from chicken chunks.
Shnupbups #2
Posted 11 May 2013 - 06:49 PM
What are the contents of the cell program?

Also, to escape from loops, use break.
To escape from entire programs, user error().
For an all rounder that's not always the best, but is good for functions, use return.
NdMSnIpEz #3
Posted 11 May 2013 - 06:56 PM
What are the contents of the cell program?

Also, to escape from loops, use break.
To escape from entire programs, user error().
For an all rounder that's not always the best, but is good for functions, use return.
The contents are here.


turtle.select(1)
turtle.place()
if turtle.detect()==true then
print("Redstone Energy Cell is Charging.")
else
  print("Redstone Energy Cell Not Recieved Yet. Waiting...")
  sleep(10)
end

m = peripheral.wrap("right")
data = m.get()
for field, value in pairs(data) do
   print(field..":"..tostring(value))  
   end
if data["Full Energy"]==true then           
turtle.dig()
turtle.select(1)
turtle.dropDown()
else
sleep(2)
end
Also, with the infinite loop, neither ctrl+t, ctrl+r or ctrl+s works. And how would you go about adding those to the startup program? (Sorry, but I'm new to computercraft and am a bit of a noob.)
Kingdaro #4
Posted 11 May 2013 - 07:15 PM
Why keep the cell program separate? Just put the cell program in a loop and run it from the console.


while true do
  turtle.select(1)
  turtle.place()
  if turtle.detect()==true then
  print("Redstone Energy Cell is Charging.")
  else
    print("Redstone Energy Cell Not Recieved Yet. Waiting...")
    sleep(10)
  end

  m = peripheral.wrap("right")
  data = m.get()
  for field, value in pairs(data) do
     print(field..":"..tostring(value))  
     end
  if data["Full Energy"]==true then           
  turtle.dig()
  turtle.select(1)
  turtle.dropDown()
  else
  sleep(2)
  end

  sleep(0.5)
end

So the startup file would be just


shell.run('cell')


Added the sleep(0.5) so that it doesn't bug out.
NdMSnIpEz #5
Posted 11 May 2013 - 07:38 PM
Thanks ^^
Worked like a charm! :)/>