LDraw.org Discussion Forums
[LDCad] setEvent, 'run' and passing parameters to macro? - Printable Version

+- LDraw.org Discussion Forums (https://forums.ldraw.org)
+-- Forum: LDraw Programs (https://forums.ldraw.org/forum-7.html)
+--- Forum: LDraw Editors and Viewers (https://forums.ldraw.org/forum-11.html)
+--- Thread: [LDCad] setEvent, 'run' and passing parameters to macro? (/thread-25325.html)



setEvent, 'run' and passing parameters to macro? - David Manley - 2021-09-25

Hi Roland,

with 1.7 scripting API in LDCad, is it possible to pass an argument to a macro when it is run? For example, if I have a macro named:
Code:
linkage_parts_select_gf

which has a single argument
Code:
first_call_ai

can a value be passed via the setEvent API e.g.
Code:
macro_lo:setEvent('run', 'linkage_parts_select_gf', '1')

Regards,

David


RE: setEvent, 'run' and passing parameters to macro? - Roland Melkert - 2021-09-25

(2021-09-25, 1:45)David Manley Wrote: with 1.7 scripting API in LDCad, is it possible to pass an argument to a macro when it is run?

Not at the moment.

I kept the callbacks simple (like in Javascript etc).

But the problem in your project can be handled using a global variable, like:

Code:
initTest=true function runTest()   if initTest then     ldc.dialog.runMessage('init')     initTest=false   end     ldc.dialog.runMessage('work') end function register()   local macro=ldc.macro('test')   macro:setEvent('run', 'runTest') end register()

Globals in any script will be preserved until the containing script is reloaded as each script runs in it's own environment.


RE: setEvent, 'run' and passing parameters to macro? - Roland Melkert - 2021-09-25

(2021-09-25, 1:45)David Manley Wrote: can a value be passed via the setEvent API

Side note:
I have been thinking about adding a single extra optional parameter to all the setEvent functions.

But it will be a table name so you could have something like:
Code:
test={   doInit=true,     init=function(self)     print('init')     self.doInit=false   end,   run=function(self)     if self.doInit then       self:init()     end         print('work')   end } function register()   local macro=ldc.macro('test')   macro:setEvent('run', 'run', 'test') end register()

To fake object oriented programming.