Machsupport Forum
Mach Discussion => Mach4 General Discussion => Topic started by: dbt3000files on November 13, 2018, 10:48:24 PM
-
Does anyone know the best way to terminate a lua script or macro?
I have been using "do return end", but I have run into a problem. If I run "do return end" from within a function it will end that function but not the entire macro script.
For example:
In a file called macrofunctions I could have the following:
function whatIwanttosay()
wx.wxmessagebox("good to see you")
do return end --end script so nothing else is said (this is what I was hoping I could use to stop the entire program)
end
function whatIamthinking()
wx.wxmessagebox("looks like you gained a few pounds")
end
My macro would be:
function m90001()
whatIwanttosay()
whatIamthinking()
end
The do return end at the end of the function whatIwanttosay() does nothing to prevent the macro from calling the next function. Is there a way to end an entire macro from within a function? If not, I am wondering what is considered best practice for ending a script.
I realize that I could have each function return a number and I could check for that number before running any further functions, but it doesn't seem like a very elegant solution. I have quite a few nested functions, and the whole thing becomes a mess if I have to query each one to see if it has completed before continuing.
Thanks for any help!!
David
-
I realize that I could have each function return a number and I could check for that number before running any further functions
Thats the route I would take......
DazTheGas
-
Thanks Daz. I'm glad I'm not just missing something obvious. So am I right in thinking the best way to do this is to use a return code? I am using return nil to end everything which is what seems to be the normal way to do it, but the examples in the Mach4 Lua manual use do return end. Any thoughts on this?
Thanks!
David
function test()
if 1 == 1 then
return 1, mc.MERROR_NOERROR
else
wx.wxmessagebox("nope")
return nil, nil
end
local answer
local rc
answer, rc = test()
if rc ~= mc.MERROR_NOERROR then
return nil
end