Hello Guest it is April 25, 2024, 07:16:54 PM

Author Topic: Understanding Lua  (Read 7628 times)

0 Members and 1 Guest are viewing this topic.

Understanding Lua
« on: December 18, 2016, 09:54:39 PM »
i note in the screen for probing for  Measure Z that Prb.SingleSurfZ  is the function called but in
the probing module it is called Probing.SingleSurfZ     How can that be?

THanks
Keith

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Understanding Lua
« Reply #1 on: December 19, 2016, 05:25:38 AM »
Prb is a derived class (or module for lua) of Probing.

when requiring a module the module name is replaced by what we tell it, in this case its prb, so although you will see Prb.SingleSurfZ its actually running Probing.SingleSurfZ.

seems weird but look at it this way, lets take a module that creates a button called btnCreate and within that module there is a function btnCreate.createButton(coords etc)

we need 3 buttons so we can do this

local btn1 = require btnCreate
local btn2 = require btnCreate
local btn3 = require btnCreate



btn1.createButton(coords etc)
btn2.createButton(coords etc)
btn3.createButton(coords etc)

so btn1 -2 and 3 becomes derived classes/modules of btnCreate.

DazTheGas
« Last Edit: December 19, 2016, 05:29:25 AM by DazTheGas »
New For 2022 - Instagram: dazthegas
Re: Understanding Lua
« Reply #2 on: December 19, 2016, 07:12:24 AM »
Ok, so to call a function in a module from a script, we would be correct for instance to call Probing.SingleSurfZ in the probing module to use mcProbing.SingleSurfZ in a screen button? but here they have used require to switch mcProbing to prb.  

Looking at mcTouchOff.lua module, the functions are named without a probing or Touch prefix.  This is confusing.  function TouchOffZNeg0 would be called from a button script by mcTouchOff.TouchOffZNeg0   Or we could use

local TO = require "mcTouchOff"

then call TO.TouchOffZNeg0 in the script

Is that correct?

Appreciate the help!!

Re: Understanding Lua
« Reply #3 on: December 19, 2016, 07:16:38 AM »
Daz, the module has a name like mcTouchOff.lua    Within the module are functions with names.  In the Probing module they put Probing. in front of every function.  In the mcTouchOff.lua module they just name the functions without a prefix.  What is the proper way to call a function in a module versus calling a function in my own script like load script or plc script.   

Thanks
Keith

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Understanding Lua
« Reply #4 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.

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
« Last Edit: December 19, 2016, 08:44:11 AM by Chaoticone »
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Understanding Lua
« Reply #5 on: December 19, 2016, 10:23:11 AM »
Nice example ;-)

Simple way to put it is Private and Public Functions

No prefix = Private function being used internally by the module and cannot be run from outside of the script
With prefix = Public function that can be run/called externally using the prefix you gave it during requiring.

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Understanding Lua
« Reply #6 on: December 19, 2016, 11:50:55 AM »
That helps very much.  I will work with it.  As I have said in the past, so much to learn!  But once learned, so much power over the machine!  Satisfying when it works!

Keith
Re: Understanding Lua
« Reply #7 on: December 19, 2016, 12:50:21 PM »
I made a lua file in the mach4hobby/modules directory called mcUserScript.lua containing the script below--

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

Flummoxed right off the bat...

Keith

Then when mach4 loads there is an error that no such module exists

Offline DazTheGas

*
  •  778 778
  • DazTheGas
    • View Profile
Re: Understanding Lua
« Reply #8 on: December 19, 2016, 01:00:33 PM »
You will need to make sure the path to the module  is included before the require, this is already set in the screen load script but still ok to put again

Code: [Select]
    local profile = mc.mcProfileGetName(inst)
    local path = mc.mcCntlGetMachDir(inst)
   
    package.path = path .. "\\Profiles\\" .. profile .. "\\Modules\\?.lua;" .. path .. "\\Modules\\?.lua;"

when using require it will look in these path locations for the module

DazTheGas
New For 2022 - Instagram: dazthegas
Re: Understanding Lua
« Reply #9 on: December 19, 2016, 04:48:08 PM »
Got it now.  I had mc.UserScript instead of mcUserScript

Thanks!