Thank you for your time. Here's what I ended up doing:
A function to ask if the machine hasn't been disabled or stopped, used each time there is a signal change or any other physical or Mach-related change, except some specific cases, more on them later.
function toolchange_machine_enabled()
local inst = mc.mcGetInstance()
local mcState = mc.mcCntlGetState(inst)
if mcState==mc.MC_STATE_IDLE then
return false
else
return true
end
end
An example of its usage:
function toolchange_boringhead_motorOFF()
if toolchange_machine_enabled()==true then
local inst = mc.mcGetInstance()
mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), false)
end
end
The reason I didn't return out of the script if this function returned false, is that G-code states (like whether G90 or G91 is currently active) get recorded at the beginning of my M6 script, and have to be returned to afterwards. The script is complex enough that is actually has its own G-code safe start line similair to one in a G-code program. This is also the specific case during which I don't check if the machine hasn't been stopped - I need the machine returned to the previous state regardless of what happens in the meantime.
For safety regarding mc.mcCntlGcodeExecuteWait(), I made this function that I replaced every direct call of mc.mcCntlGcodeExecuteWait() with:
function toolchange_callGcode(gcode)
local inst = mc.mcGetInstance()
local rc = mc.mcCntlGcodeExecuteWait(inst, gcode)
if rc ~=0 then
mc.mcCntlEnable(inst, false)
mc.mcCntlSetLastError(inst, "Something went wrong with executing G-code from within M6. The machine was disabled for safety. Error state: "..rc)
end
end
Knowing the rest of my code can handle the machine stopping, I stop the machine directly from this function, which also saves me from having to track another error flag.
I also found out that if you call a custom M-script from within another custom M-script, Mach freezes, not taking any G-code anymore, yet not stating that there was an error, jogging also retains its function. Consider I was using Mach4 version 4.2.0.2872, and newer versions may not have this issue.