Hello Guest it is April 27, 2024, 07:10:25 PM

Author Topic: How do I call a function in a module from a screen button?  (Read 160 times)

0 Members and 1 Guest are viewing this topic.

How do I call a function in a module from a screen button?
« on: March 08, 2024, 07:46:04 PM »
Like the title states I'm trying to call a function that I have placed in a module from a button I've added to a screen.

I plan to add several functions and but even though I've copied examples that I've found they do not work. So far my code is really simple it looks like:

Button: (Clicked Script event)
Code: [Select]
PrintVal("123")
In folder C:\Mach4Hobby\Profiles\AXYZ\Macros
I added File: Load_Modules.mcs, all of the code looks like:
Code: [Select]
package.path = wx.wxGetCwd() .. "\\Profiles\\AXYZ\\Modules\\Test.lua"

package.loaded.Test = nil
require "Test"

In Folder C:\Mach4Hobby\Profiles\AXYZ\Modules
I added File: Test.lua, all of the code looks like:
Code: [Select]
function PrintVal(val)
wx.wxMessageBox("Current val="..tostring(val))
end

When I press the button I get the error:
Lua: Error while running chunk
c:\Mach4Hobby\ScreenScript.lua:854: attempt to call a nil value (global 'PrintVal')
stack traceback:
c:\Mach4Hobby\ScreenScript.lua:854: in function 'BUTTON_Test_Clicked_Script'


Can anyone tell me what I am doing wrong?
All of the files compile. The best I can figure out is that the screenscript.lua doesn't know anything about the modules, but according to the documentation my Load_modules.mcs file should load this.

I have made sure to delete the Load_Modules.mcc file and restart mach to recompile it after making changes to that file.
Re: How do I call a function in a module from a screen button?
« Reply #1 on: March 09, 2024, 04:48:51 AM »
You're getting a bit confused.. but are almost there

The Load_modules.mcs file is for loading modules into the lua chunk that exists for macros only. This is separate to the chunk that runs the screenload/PLC/signal scripts etc.

To call the module functions from a button you need to include the following at the top of the screenload script as well:

Code: [Select]
package.loaded.Test = nil
t = require "Test"  --This stores the contents of the module into table 't' or a name of your choice

Your module 'Test.lua' should look like this:

Code: [Select]
local Test = {}

function Test.PrintVal(val)
          wx.wxMessageBox("Current val="..tostring(val))
end

function Test.AnotherFunction()
           wx.wxMessageBox("This is another function")
end

return Test

Now in your button script you can call the functions like this, note the use of 't' before the function name as this is where we stored them when we 'required' the module
Code: [Select]
t.PrintVal(val)

t.AnotherFunction()


Because you have also created the Load_modules.mcs and put it in your macros folder, this will load the same module in the macro lua chunk so you can call the functions from your macros (m6, m3 etc) as well. Please note that you cannot use the module to transfer information between the GUI lua chunk and the macro lua chunk, only registers can do this.

Hopefully this helps
Re: How do I call a function in a module from a screen button?
« Reply #2 on: March 09, 2024, 11:41:03 AM »
SwiftyJ, clearest response I have seen over the many years that Mach4 has been around regarding modules and their uses, even got the registers in there!
Bravo!
Re: How do I call a function in a module from a screen button?
« Reply #3 on: March 10, 2024, 04:06:21 PM »
Thank you so much for your explanation! It was nice and clear! And worked exactly like I needed it to.