571
Mach4 General Discussion / Re: Understanding Lua
« on: December 19, 2016, 08:41:54 AM »
Let me try to help and hopefully Daz will correct any mistakes I make.
The reason mcTouchOff's functions do not have a prefix (mcTouchOff.) is because none of its functions are called or available for use from outside the module. The only one that is, is the Dialog which is called from outside the module. It opens the dialog or user interface. Any other functions are only called form it. Not that the functions could not be used from outside the UI but from a support stand point its not something we wanted to do.
I did this example a few days ago. Its a very simple module that simply pops up a message box and displays the message parameter you pass it. You have 5 things to do to see all of.
1) Create and save a module.
2) Load the module with the screen so it can be used form screen elements.
3) Load the module with the macros so it can be called from macros (Mcodes).
4) Call the module passing it your message from a button.
5) Call the module passing it your message form a custom Mcode (m110 in the example).
Play with it. Break it, fix it, add to it and have some fun.
The reason mcTouchOff's functions do not have a prefix (mcTouchOff.) is because none of its functions are called or available for use from outside the module. The only one that is, is the Dialog which is called from outside the module. It opens the dialog or user interface. Any other functions are only called form it. Not that the functions could not be used from outside the UI but from a support stand point its not something we wanted to do.
I did this example a few days ago. Its a very simple module that simply pops up a message box and displays the message parameter you pass it. You have 5 things to do to see all of.
1) Create and save a module.
2) Load the module with the screen so it can be used form screen elements.
3) Load the module with the macros so it can be called from macros (Mcodes).
4) Call the module passing it your message from a button.
5) Call the module passing it your message form a custom Mcode (m110 in the example).
Play with it. Break it, fix it, add to it and have some fun.
Code: [Select]
--This is the mcUserScript.lua module that goes in the modules folder
local mcUserScript = {}
function mcUserScript.UserMessage(Message)
if (Message == nil) then --No message was passed
Message = "No message passed" --If no message is passed this will be the default message
end
wx.wxMessageBox(Message)
end
return mcUserScript -- Module End
--This is what loads the module above.
--Add this to the modules load section of the screen load script so you can call its functions from buttons on the screen.
--Add this to the load_modules.mcs file in the macros folder of the profile that will be using it so you can call its functions from M codes.
package.loaded.mcUserScript = nil
us = require "mcUserScript"
--Put this in a buttons clicked script and when you click it a message box will pop up that says... No message passed
us.UserMessage() --This is not passing a message for the Message parameter so the message will be the default
--m110 goes in the macros folder of the profile that will be using it
function m110()
us.UserMessage("This is my message")
end
if (mc.mcInEditor() == 1) then
m110()
end