Jump to content




Lua question


22 replies to this topic

#1 grand_mind1

  • Members
  • 207 posts

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


#2 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

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:)

#3 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

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


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

View PostWillibilly19, on 21 January 2013 - 06:50 PM, said:

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. :)

#5 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 06:55 PM

View PostWillibilly19, on 21 January 2013 - 06:48 PM, said:

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


#6 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

Posted 21 January 2013 - 06:57 PM

View Postbrett122798, on 21 January 2013 - 06:55 PM, said:


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.

#7 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 06:59 PM

View PostWillibilly19, on 21 January 2013 - 06:57 PM, said:

View Postbrett122798, on 21 January 2013 - 06:55 PM, said:

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.

#8 grand_mind1

  • Members
  • 207 posts

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.

#9 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 21 January 2013 - 07:12 PM

View Postgrand_mind1, on 21 January 2013 - 07:09 PM, said:

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()


#10 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 07:15 PM

Ok! Thanks for your help! :D

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 07:22 PM

View Postbrett122798, on 21 January 2013 - 07:12 PM, said:

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()

#12 Superiorchaos

  • Members
  • 1 posts

Posted 21 January 2013 - 07:33 PM

I may be wrong but I beleive you can use shell.exit(), or os.reboot()

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 07:46 PM

View PostSuperiorchaos, on 21 January 2013 - 07:33 PM, said:

I may be wrong but I beleive you can use shell.exit(), or os.reboot()
Never! Ever! Especially shell.exit() ...

#14 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

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.

#15 grand_mind1

  • Members
  • 207 posts

Posted 21 January 2013 - 08:22 PM

View PostWillibilly19, on 21 January 2013 - 07:59 PM, said:

Can I ask why? I have never used them...but I'm really curious why those warrant a response like that.
As am I...

#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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
Spoiler

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...

#17 Willibilly19

  • Members
  • 48 posts
  • LocationColorado, USA

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.

#18 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 10:52 PM

View PostWillibilly19, on 21 January 2013 - 10:46 PM, said:

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

#19 ChunLing

  • Members
  • 2,027 posts

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 by ChunLing, 21 January 2013 - 10:54 PM.


#20 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 January 2013 - 11:05 PM

View PostChunLing, on 21 January 2013 - 10:53 PM, said:

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...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users