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

Lua question

Started by grand_mind1, 21 January 2013 - 05:45 PM
grand_mind1 #1
Posted 21 January 2013 - 06:45 PM
Is there a way to make a program terminate inside of a program? Like:

i = 1
if 1 = 1 then
    --this is where I would want it to terminate it
else
    print("NO!")
end
Willibilly19 #2
Posted 21 January 2013 - 06:48 PM
are you just meaning to break the loop?

If so give break a try. like this maybe. (not 100% sure, but it's worth a shot)


i = 1
if i == 1 then
   break			  --this is where I would want it to terminate it
else
  print("NO!")
end


Don't listen to this post. I made a mistake and there are better ways below:)
Willibilly19 #3
Posted 21 January 2013 - 06:50 PM
oops, sorry. I realized that it was an if statement and not a loop. forgive me, but that won't work for if statements.



another way to do it though would be just add sleep(0) in there. might not be a great way, but it'd work.


i = 1
if i == 1 then
   sleep(0)		 --this is where I would want it to terminate it
else
  print("NO!")
end
Lyqyd #4
Posted 21 January 2013 - 06:51 PM
If it's not inside a function in the program, you can use return to exit the program, otherwise you'd need to use error() or a specific return value from the function, checked for by the calling code.

oops, sorry. I realized that it was an if statement and not a loop. forgive me, but that won't work for if statements.

You are able to edit your posts to make such corrections. :)/>
brett122798 #5
Posted 21 January 2013 - 06:55 PM
are you just meaning to break the loop?

If so give break a try. like this maybe. (not 100% sure, but it's worth a shot)


i = 1
if i == 1 then
   break			  --this is where I would want it to terminate it
else
  print("NO!")
end
Uhh.. are you sure you can use break in an if statement? Did you want to get out of the if statement, or exit the program? If it's the first thing I said.. you could do this:


if somevar = true then
if permission ==  true then
-- Stuff
else
-- This happens if you want to exit out of the if statement (Optional)
end
end
Willibilly19 #6
Posted 21 January 2013 - 06:57 PM
Uhh.. are you sure you can use break in an if statement? Did you want to get out of the if statement, or exit the program? If it's the first thing I said.. you could do this:


No, you can't it was a mistake. I just woke up (good excuse?:P/>) I still don't have a vast knowledge of LUA, but I corrected myself.
brett122798 #7
Posted 21 January 2013 - 06:59 PM
Uhh.. are you sure you can use break in an if statement? Did you want to get out of the if statement, or exit the program? If it's the first thing I said.. you could do this:


No, you can't it was a mistake. I just woke up (good excuse? :P/>) I still don't have a vast knowledge of LUA, but I corrected myself.
I see that now, I got ninja'd while I making my reply.
grand_mind1 #8
Posted 21 January 2013 - 07:09 PM
Either I don't understand what you are saying or I didn't explain it well enough. What I would try to use this for is in a refuel function like this:

fuel = turtle.getFuelLevel()
function checkfuel()
	if fuel < 100 then
         --terminate
	else
         return
	end
end
mining stuff
checkfuel()	
Now that I'm thinking about it, it is probably pretty dumb to terminate the program instead of waiting for fuel, but now I'm curious.
brett122798 #9
Posted 21 January 2013 - 07:12 PM
Either I don't understand what you are saying or I didn't explain it well enough. What I would try to use this for is in a refuel function like this:

fuel = turtle.getFuelLevel()
function checkfuel()
	if fuel < 100 then
		 --terminate
	else
		 return
	end
end
mining stuff
checkfuel()	
Now that I'm thinking about it, it is probably pretty dumb to terminate the program instead of waiting for fuel, but now I'm curious.
Yeah, the quick and dirty way of doing that would be error().


fuel = turtle.getFuelLevel()
function checkfuel()
	if fuel < 100 then
		 error("Fuel Ran Out!") --terminate
	else
		 return
	end
end
mining stuff
checkfuel()
grand_mind1 #10
Posted 21 January 2013 - 07:15 PM
Ok! Thanks for your help! :D/>
theoriginalbit #11
Posted 21 January 2013 - 07:22 PM
Yeah, the quick and dirty way of doing that would be error().
Why do you say it's dirty? It's the best way, especially if your lots of function calls deep, else you would have to return something the whole way back through the stack, and have an if statement on each function call, all just to get to the top level function to return again… Much easier to code it if you use error()
Superiorchaos #12
Posted 21 January 2013 - 07:33 PM
I may be wrong but I beleive you can use shell.exit(), or os.reboot()
theoriginalbit #13
Posted 21 January 2013 - 07:46 PM
I may be wrong but I beleive you can use shell.exit(), or os.reboot()
Never! Ever! Especially shell.exit() …
Willibilly19 #14
Posted 21 January 2013 - 07:59 PM
Can I ask why? I have never used them…but I'm really curious why those warrant a response like that.
grand_mind1 #15
Posted 21 January 2013 - 08:22 PM
Can I ask why? I have never used them…but I'm really curious why those warrant a response like that.
As am I…
theoriginalbit #16
Posted 21 January 2013 - 10:43 PM
Sorry bout the late reply… Damn phone wouldn't send the reply then I dropped out if reception…

Ok I want you to do something for me. I want you to try to exit the running program using shell.exit()
Try it in the Lua prompt. What happens?
Answer
SpoilerWow! Nothing happened, I typed it in and the Lua prompt is still open. Huh that function mustn't work then *exits Lua prompt* … Woah! Where's my terminal?! Where's the shell?! … Oh wait I just terminated it with shell.exit() … In summary shell.exit() should never be used to close a running program, because well it doesn't!

Now as for the os.reboot() I'm gunna have to see if I can find the really good explanation that was done a while back in here as to why not to use it…
Willibilly19 #17
Posted 21 January 2013 - 10:46 PM
Sorry for the really noobish reply. When I asked, I didn't know what the shell was, but I have since looked it up and do understand the reasoning behind your statement.
theoriginalbit #18
Posted 21 January 2013 - 10:52 PM
Sorry for the really noobish reply. When I asked, I didn't know what the shell was, but I have since looked it up and do understand the reasoning behind your statement.
It's ok was also answering it for grand_mind
ChunLing #19
Posted 21 January 2013 - 10:53 PM
os.reboot is fine. shell.exit causes you to exit the shell and shutdown…it should also be fine.

There is a cooldown on shutdown/reboots, so there can be performance issues from using either if a computer is doing something important.

Oh, yeah…the shell exit doesn't take effect until after you exit your current program, so it doesn't do what you want. I just meant it doesn't do anything terrible.
Edited on 21 January 2013 - 09:54 PM
theoriginalbit #20
Posted 21 January 2013 - 11:05 PM
There is a cooldown on shutdown/reboots, so there can be performance issues from using either if a computer is doing something important.
Maybe that's what who ever was talking about…
ChunLing #21
Posted 21 January 2013 - 11:29 PM
Well, the reason they imposed a cooldown is because rebooting is pretty expensive, relatively speaking. So programs that used rebooting as a "easy" way to loop themselves were being laggy and evil to the rest of the world's computers.
theoriginalbit #22
Posted 21 January 2013 - 11:50 PM
Well, the reason they imposed a cooldown is because rebooting is pretty expensive, relatively speaking. So programs that used rebooting as a "easy" way to loop themselves were being laggy and evil to the rest of the world's computers.
Which in itself really is a reason not too do it…
ChunLing #23
Posted 22 January 2013 - 08:24 AM
Or was, anyway. Now the reason is the cooldown, which is long enough to obviate the expense of a reboot to the overall CC computer capacity (and discourage using reboot just to infinite loop a program rather than when you actually want to reboot). There are certainly better ways to simply exit a program if you have no need to reload everything, but reboot is no longer evil…it's just slow.