Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: bcoop on March 13, 2020, 09:06:04 AM

Title: Passing a value in function call
Post by: bcoop on March 13, 2020, 09:06:04 AM
New to programming lua, need some basic help with passing a parm to a function, 
trying to set the JogRatePercent by calling a function and passing the float value i want to set in the JogRatePercent.
here is what i have which is not working. 
any help would be appreciated.

function JogRatePercent(percent) --This function sets the jog rate

wx.wxMessageBox(percent)
   scr.SetProperty("droJogRate", "Value", 'percent')
end

--------------------------------------------------------------------------------------
--the code below works but without passing the value to the function.


function JogRate1Percent() --This function sets the jog rate to 1 %

   scr.SetProperty("droJogRate", "Value", "1.0")
end
JogRate1Percent()
Title: Re: Passing a value in function call
Post by: SwiftyJ on March 13, 2020, 11:26:15 AM
The issue is that you have single quotation marks around percent. Lua recognises anything enclosed with " " or ' ' as strings so its trying to set it to the string 'percent'. If passing the param to the function as a number you'll have to use the tostring() function as scr.SetProperty requires strings.

Try this..
Code: [Select]
function JogRatePercent(percent) --This function sets the jog rate
   scr.SetProperty("droJogRate", "Value", tostring(percent))
end

Ideally you should use the API call mc.mcJogSetRate rather than using the scr call. If you haven't found the list of these API's yet check inside the Docs folder in the Mach4 directory, Mach4CoreAPI.chm.

wx.wxMessageBox() also only accepts strings.
Title: Re: Passing a value in function call
Post by: bcoop on March 13, 2020, 11:58:19 AM
Works!   Thank you very much SwitftyJ,  much appreciated.