Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exiting and Restarting a Function in Lua

Tags:

function

lua

My problem is that I have a function a() that I need to exit unconditionally at certain times. My question is how would I achieve the goal of exiting a function without conditions.

Example:

 local function a()
   --Do Stuff
 end

 a()

Another words, is there a way to exit a() with a single command?

like image 784
user3496349 Avatar asked Jul 18 '26 13:07

user3496349


1 Answers

There are only few ways to "exit" a function: return, coroutine.yield (if the function is executed as a coroutine), and error() call (which you can trap with pcall around the function).

I'm not sure what you mean by "exiting a function without conditions", but one of these options should work for you as there is nothing else available. (You can also use debug hook and call error() from it without modifying your function code at all, but I would not expect this to be needed in your case.)

like image 194
Paul Kulchenko Avatar answered Jul 20 '26 18:07

Paul Kulchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!