Hi!
I'm using CC1.5. Maybe (probably) I'm just glossing over something... Or it's a programming trick I don't know... How do I terminate a program from inside a program? For example if I press the Q key (os.pullEvent()) the program stops running and then the CraftOS1.5 comes back. How do I do that with a plain grey computer?
Thank you for your answer!
How to exit a program?
Started by eastar, Jun 16 2015 01:56 AM
5 replies to this topic
#1
Posted 16 June 2015 - 01:56 AM
#2
Posted 16 June 2015 - 02:00 AM
Well, if you aren't in a function, you can call "return". Personally I just call "error()", which works anywhere.
#3
Posted 16 June 2015 - 02:21 AM
Bomb Bloke, on 16 June 2015 - 02:00 AM, said:
Well, if you aren't in a function, you can call "return". Personally I just call "error()", which works anywhere.
@easter:
The program should just terminate by reaching the end or returning early. In this manner, your programs is exactly like a function. You retrieve arguments via "..." and can return in any point of your program. Note, that you cannot return directly when inside a nested function.
In your example, the most basic solution would be to "break" out of your event loop:
while(true) do
e, a, b, c = os.pullEvent()
if(e == "char" and a == "q") then
break --# this will effectively jump to ....
end
...
end
--# this point ;)/>
...
#4
Posted 17 June 2015 - 02:37 PM
Thank you the answers! It was just too late and I was too impatient to think about return() and break().
Thanks again!
Thanks again!
#6
Posted 17 June 2015 - 04:04 PM
while true do
-- do stuff
if something then
break
end
end
function foo()
return "wow"
end
print(foo())
--> "wow"
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











