Hello Guest it is March 28, 2024, 06:39:16 AM

Author Topic: Best practice to communicate between Lua scripts?  (Read 1503 times)

0 Members and 1 Guest are viewing this topic.

Best practice to communicate between Lua scripts?
« on: June 14, 2018, 04:47:41 PM »
I have a state flag that I need to be accessible in multiple scripts.

It looks like this could be done by creating a register in the register plugin or I could create a LED on the screenset and use its state to communicate. Or ??

Related questions
-- what are the differences between an "instance" and a "global" register?
-- is there a hook to trigger or launch a script when a new g-code file has been loaded?
 
Re: Best practice to communicate between Lua scripts?
« Reply #1 on: June 15, 2018, 03:07:55 AM »
Hi,
I think you'll have to use registers, indeed much of the reason for having them is to communicate between Lua chunks.

Mach has at least two Lua chunks, the GUI and the Cgode interpreter. They cannot run simultaneously and so in any session of Mach control passes back and forth
between these two chunks. No variables in one chunk are available in the other. Registers are required.

A related idea is that if you require Mach execute a movement say, then Mach's control state must be MC_STATE_IDLE (as opposed to in use). You might require an MDI move say, so Machs
control state would have to be MC_STATE_IDLE and then when the MDI started to execute it would change to MC_STATE_MRUN. If you now wish to access the GUI and execute some functions there you will have
to find the means of wresting control from the MC_STATE_MRUN state back to MC_STATE_IDLE and THEN cause the GUI chunk to run. You have found that a RESET is sometimes required to wrest control from the current chunk.

http://www.machsupport.com/forum/index.php/topic,36548.0.html

I have a list of Machs states as a text document and put it in the Docs folder that I might have it at hand when coding. I have found that at certain times it is necessary to determine Machs current state
before you attempt certain calls, otherwise the call will fail, sometimes unpredictably and sometimes you can crash Mach.

My understanding of this stuff is very rudimentary at best but even a little understanding can demystify Mach considerably.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Best practice to communicate between Lua scripts?
« Reply #2 on: June 15, 2018, 07:39:19 PM »
Craig, you are too humble. And thank you.
-- Paul
Re: Best practice to communicate between Lua scripts?
« Reply #3 on: June 16, 2018, 05:40:27 AM »
Paul

You could also try using one of the global pound variables #100--#199 as a flag. These avoid the need to  create registers. I do this to exchange data between my M6 macro and the screen script and haven't found any issues with it.

Allan
« Last Edit: June 16, 2018, 05:44:44 AM by Fledermaus »
Re: Best practice to communicate between Lua scripts?
« Reply #4 on: June 16, 2018, 06:55:31 AM »
Thanks Allen!