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

How to properly terminate the program from a subroutine

Started by grizzlebizzle, 24 August 2012 - 07:02 PM
grizzlebizzle #1
Posted 24 August 2012 - 09:02 PM
What is the command I would use to do this? The first thing I tried calling ended up shutting down the OS. Since then I have tried a few other things the latest of which is shell.exit() which just seems to hang the program (not very helpful). I would like to return to a prompt without having to hold CTRL-T to stop the program.
Jan #2
Posted 24 August 2012 - 09:25 PM
I dont fully understand the situation… with 'subroutine' you mean a program right?
In that case you can try this:

return
Sorry if it doesnt answer your question
grizzlebizzle #3
Posted 24 August 2012 - 09:43 PM
No I mean a local procedure… which may be called from another procedure which may be called from another procedure. For example


local function foo()
   bar()
end

local function bar()
   -- something happened and I need to terminate the program here... how do I do it?
end

foo()


The return statement is not going to work (only return from the top-most level will terminate the program).
Jan #4
Posted 24 August 2012 - 11:21 PM
No I mean a local procedure… which may be called from another procedure which may be called from another procedure. For example


local function foo()
   bar()
end

local function bar()
   -- something happened and I need to terminate the program here... how do I do it?
end

foo()


The return statement is not going to work (only return from the top-most level will terminate the program).

Do you mean with 'something happened' that there went something wrong? Try this:

error("something went wrong")
This stops the program and shows the line-number and and the error-message in the shell
SwitchLove #5
Posted 25 August 2012 - 05:39 AM
I think what he means is this, Ill use an example of how I need it.

For example a doorlock, its set as startup so if done right the right password opens the door then reset the program so its asking for the password again, now say I throw in an "Admin/Debug" password, how would you get it to close out of the program to be at the command prompt part again?


if input == password then
 print("Door Opened")
 rs.setOutput(side,true)
 sleep(2)
 rs.setOutput(side,false)
elseif input == debug then
 error("Command Prompt Accessed")

Where debug is the debugging password, because having to teminate my program to edit it is annoying, is there a way to do it like the error but without the prog name and line number?
ardera #6
Posted 25 August 2012 - 08:03 AM
You can use error() (yes, no params) it will terminate the program at this line and will print no line and program.
SwitchLove #7
Posted 26 August 2012 - 12:02 AM
Cool thanks.