Hello Guest it is March 29, 2024, 10:41:59 AM

Author Topic: Here is a way to pass a Variable number of Params to a Lua function  (Read 3728 times)

0 Members and 1 Guest are viewing this topic.

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Here is a way to pass a Variable number of Params to a Lua function.

function VariableParamFunc(...)
   local p1 = 0;
        local p2 = 0;
        local p3 = 0;
        local mInst = 0;
        local inst = mc.mcGetInstance(mInst);

   p1, p2, p3 = select(1, ...);--this means start with the 1st param, and get
        --all that is passed, in this case a max of three are passed, so p1-3  
       --receives them.
        
        if p1 == nil then p1 = "nil"; end
        if p2 == nil then p2 = "nil"; end
        if p3 == nil then p3 = "nil"; end    
   mc.mcCntlSetLastError(  inst,
                                           "p1 = " .. tostring(p1)..
                                           " p2 = ".. tostring(p2)..
                                           " p3 = ".. tostring(p3));   
end

if (mc.mcInEditor() == 1) then
    VariableParamFunc();
    VariableParamFunc(2,3,4);
end

--You can pass as many params as you want, by putting in the function
--variables that get them via the order them come in at.
--this example handles three.

The website screws up the formatting, so the file is attached.

Scott
« Last Edit: December 02, 2014, 02:55:27 PM by poppabear »
fun times

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Here is a way to pass a Variable number of Params to a Lua function
« Reply #1 on: December 02, 2014, 07:04:44 PM »
Hya Scott, Can your method be used with a Mcodes  such as M701 . If so can you give an example (;-).

(;-) TP

Offline BR549

*
  •  6,965 6,965
    • View Profile
Re: Here is a way to pass a Variable number of Params to a Lua function
« Reply #2 on: December 02, 2014, 09:38:56 PM »
HIYA SCott, I think I found the answer to that question. In Mach4 you cannot use the ( ) for anything other than comments in Gcode. AND mach4 woul dhave to be modified to anything else.

Thanks , (;-) TP

Offline poppabear

*
  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
Re: Here is a way to pass a Variable number of Params to a Lua function
« Reply #3 on: December 03, 2014, 07:14:15 AM »
Yes, it is only for using inside a script.
It cannot be used (at this time), in the MDI or G-Code,
but....  you can pass local #vars to a User Macro in the MDI or G-Code.

Scott
fun times