i = 1
if 1 = 1 then
--this is where I would want it to terminate it
else
print("NO!")
end
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Lua question
Started by grand_mind1, 21 January 2013 - 05:45 PMPosted 21 January 2013 - 06:45 PM
Is there a way to make a program terminate inside of a program? Like:
Posted 21 January 2013 - 06:48 PM
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:)
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.
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
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.
You are able to edit your posts to make such corrections. :)/>
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. :)/>
Posted 21 January 2013 - 06:55 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: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
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
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.
Posted 21 January 2013 - 06:59 PM
I see that now, I got ninja'd while I making my reply.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.
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.Posted 21 January 2013 - 07:12 PM
Yeah, the quick and dirty way of doing that would be error().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: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.fuel = turtle.getFuelLevel() function checkfuel() if fuel < 100 then --terminate else return end end mining stuff checkfuel()
fuel = turtle.getFuelLevel()
function checkfuel()
if fuel < 100 then
error("Fuel Ran Out!") --terminate
else
return
end
end
mining stuff
checkfuel()
Posted 21 January 2013 - 07:15 PM
Ok! Thanks for your help! :D/>
Posted 21 January 2013 - 07:22 PM
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()Yeah, the quick and dirty way of doing that would be error().
Posted 21 January 2013 - 07:33 PM
I may be wrong but I beleive you can use shell.exit(), or os.reboot()
Posted 21 January 2013 - 07:46 PM
Never! Ever! Especially shell.exit() …I may be wrong but I beleive you can use shell.exit(), or os.reboot()
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.
Posted 21 January 2013 - 08:22 PM
As am I…Can I ask why? I have never used them…but I'm really curious why those warrant a response like that.
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
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…
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
Spoiler
Wow! 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…
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.
Posted 21 January 2013 - 10:52 PM
It's ok was also answering it for grand_mindSorry 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.
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.
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
Posted 21 January 2013 - 11:05 PM
Maybe that's what who ever was talking about…There is a cooldown on shutdown/reboots, so there can be performance issues from using either if a computer is doing something important.
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.
Posted 21 January 2013 - 11:50 PM
Which in itself really is a reason not too do it…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.
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.