Hi,
Mach4 and Lua beginner here. I've seen several related questions here but I cannot really translate the answers to my specific issue, so I start a new thread, sorry if this is a duplicate.
I've been trying something very simple for some time now and cannot get it to work, I suspect the reason is I don't understand how Lua scripts are run and how the entire Mach4 environment is set together. I made a module that I am successfully using from ScreenScript, it is made for sending OSC messages to another application via UDP. The module is placed in Modules/OSCDRO.lua and uses some Lua libraries that I added to the Mach4Hobby root folder.
Short Version:
I am trying to call a function that I defined in the ScreenScript from out of ta Module script. Is that even possible? Since I failed, I tried to use module directly in my macro and not take the detour of calling a ScreenScript-function, as I understand that the module loaded in the ScreenScript should also be available in the module (?). But I also failed with that. So, what would already solve my problem would be a way to call a function defined in the ScreenScript out of a macro.
Detailed Version:
I load and initialize it in the screen load script simply with
package.loaded.OSCDRO = nil
osc = require "OSCDRO"
oscIntervalMS = 10 --ms
--Open the osc socket
if(osc.Setup()=="") then
scr.StartTimer(0,oscIntervalMS,0)
end
In the screen timer script I send my packages as follows:
if timer == 0 then
if(machEnabled~=0) then
osc.SendXYZ()
end
end
This works fine so far. Now I would like to make use of M-Codes and call, e.g., osc.SendM(100) from my M100 macro. I tried calling a global function from the macro and could not get it to work, apparently it was not found (?), then I tried to access the osc object, but . My Profiles/Mach4Mill/Macros/m100.mcs looks as follows:
function m100()
local inst = mc.mcGetInstance()
mc.mcCntlLog(inst, "M100" ,"" ,0 )
osc=require "OSCDRO"
mc.mcCntlLog(inst, "require okay" ,"" ,0 )
if(osc == nil) then
mc.mcCntlSetLastError(inst, "osc not loaded")
end
if(osc.Setup()=="") then
osc.SendM(100)
else
mc.mcCntlSetLastError(inst, "osc setup failed")
end
end
if (mc.mcInEditor() == 1) then
m100()
end
What's particularly strange here is, that the script execution seems to stop at the line containing the "require", since I don't get any log messages that come after this line.
Note that I would actually prefer to do something like this, because I don't want another instance of osc in every macro, since I would prefer to use one single UDP sender instead of dozens:
function m100()
sendM(100) --calling function in ScreenScript, that calls osc.SendM(100)
end
if (mc.mcInEditor() == 1) then
m100()
end
What's also strange is that when I run the debugger for my m100.mcs in ZeroBrane everything works well, osc is loaded via require and SendM is called perfectly, but when I run using the "Cycle Start MDI" button, it doesn't: the macro script seems to run, but the require fails.
I don't understand this whole behavior at all, I tried so many things by now by trial-and-error, but I keep failing. I would therefore highly appreciate some advice. Apparently it has to do with something I'm not aware of, e.g., the script loading order, the interaction between ScreenScript and macros, maybe different Lua environments they are run in, execution order, some Lua syntax or scope pitfalls that I'm oblivious about... idk.
(Using Mach4Hobby 4.2.0.5036)