Lua: Calling script with (predefined) parameters

Hi guys,

This has to be a simple question again, but I just don’t get it - so thanks for your kind help!

I wrote a Lua script which works quite well and does a couple of things in one go. If I now want to only execute some parts of the script, I’d need to call it with some kind of flag parameter to tell the script what to execute and what not…

I need to bind the script calls to individual keyboard shortcuts, like: do the whole thing (default), do part A, do part B…

How would I do that? Thanks a lot!

Is the question unclear, too simple or too complicated?

As a workaround, I could duplicate the script to several files, hardcode the wanted changes and call it separately this way…?

My first thought was also to separate it into multiple parts. No clue about your original question, sorry :slightly_frowning_face:

There are two ways to achieve this.

  1. Instantiation parameters: You decide what the script does when it’s loaded (or mapped to a button)

or

  1. Runtime parameter: Every time you run the script, the script asks via a dialog what to do.

For (2) have a look at share/scripts/_dialog_test.lua – there are also various scripts using a interactive dialog.

As for (1) you can ask for parameters when the script is instantiated

function action_params ()
  -- return a table of possible parameters
end

function factory (params)
  return function ()
   -- the script is created with given parameters
  end
end

One example is share/scripts/addscopes.lua

1 Like

Dear Robin, thank you very much for this explanation, this is absolutely great!

I simply didn’t know what to do with the instantiation parameters before; you helped me understand that this all makes sense once the script is added in the script manager!!

:see_no_evil:

Thank you!