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

How to break a loop out of scope

Started by LoganDark, 02 January 2016 - 10:39 PM
LoganDark #1
Posted 02 January 2016 - 11:39 PM
I've discovered there is no way to stop a "while" loop from a function defined out of scope, for example this will not work:

function stop()
  term.setTextColor(colors.white)
  term.clear()
  term.setCursorPos(1, 1)
  break
end

while true do
  print("Exiting program in 2 seconds")
  sleep(2)
  stop()
end
Because, from the scope of the function, there is no loop to break, unless it is defined inside of the loop, as such:

while true do
  local function stop()
    term.setTextColor(colors.white)
    term.clear()
    term.setCursorPos(1, 1)
    break
  end

  print("Exiting program in 2 seconds")
  sleep(2)
  stop()
end

Unfortunately, this function can only be used inside of the loop it is defined within, and loops nested inside that use the function will end the loop the function is defined in.

I would like to be provided with a function that can be defined once and end all loops, to ultimately stop the program, but while clearing the screen and stopping all loops.
Lignum #2
Posted 02 January 2016 - 11:52 PM
You could call error without any arguments as such, which will instantly stop the program:

error()

However, in my opinion, this is a very ugly solution. I suggest you do something like this instead:


local keepRunning = true

while keepRunning do
   --# ...
end

Now if you set keepRunning to false, the loop will stop. Keep in mind that if you do this while the loop is running, the loop will finish its current iteration before it stops.
Anavrins #3
Posted 03 January 2016 - 12:02 AM
Simply use the "break" keyword

while true do
  print("Exiting in 2 seconds")
  sleep(2)
  break
end
#- Will resume execution after the "end" of the while loop, assuming it's the end of the program


Didn't read correctly, Lignum solution works for your case.
Edited on 02 January 2016 - 11:09 PM
KingofGamesYami #4
Posted 03 January 2016 - 12:20 AM
The real question here is why would you ever want to do that.
H4X0RZ #5
Posted 03 January 2016 - 01:53 AM
The real question here is why would you ever want to do that.
I guess the op want's to execute some code once you stop the loop without re-initializing the function all the time or copy-pasting the code after each "break".
Bomb Bloke #6
Posted 03 January 2016 - 02:08 AM
Yeah, but the obvious way to do that is to simply execute that code after the loop has ended, not before.
LoganDark #7
Posted 03 January 2016 - 04:50 AM
You could call error without any arguments as such, which will instantly stop the program:

error()

I'm wondering if that will stop the program without causing a newline, hmm might have to test it. Going to edit post after testing.

Edit: I have tested it and it works perfectly! Thanks!

{see above}

However, in my opinion, this is a very ugly solution. I suggest you do something like this instead:


local keepRunning = true

while keepRunning do
   --# ...
end

Now if you set keepRunning to false, the loop will stop. Keep in mind that if you do this while the loop is running, the loop will finish its current iteration before it stops.

I have tried that, and it seems to work most of the time, but I don't think this is the correct solution.
Edited on 03 January 2016 - 03:59 AM
Dragon53535 #8
Posted 03 January 2016 - 05:04 AM
How I prefer to do it, in other programming languages without using tools like error and system.exit


local function func()
  --#I want to leave my loop
  return "break" --#Or false, or whatever I want my break variable to equal
end

while true do
  local funcStuff = func()
  if funcStuff == "break" then
    break
  end
end