Newbie question: interaction between script and UI?

Reading developers manual and forum’s examples of scripts I’m still struggling to understand the logic of how interaction between script and UI is provided. Say, I want it to be carried out only by the means of script (without typing-in addresses for parameters in Macro Page Designer). To my understanding I am to create a Parameter via defineParameter function. Let it be like this

defineParameter(mySwitch, nil, 0, 1, 1) (by the way what for Long Name is used?)

Now I can see the newly created parameter in the Parameters List and can read or change its value from the script via getParameter() and setParameter().

After it I create a button on Macro Page. Now how can I relay mySwitch value to the button’s state and vice verse – relaying button’s state to mySwitch? How can it be done with less of drag-n-dropping, but via script only?

You can’t. That’s how it works in Halion. But it also has some advantages. You can create a macro page without any scripting at all.

getParameter and setParameter are useful when you want to change Halion engine parameter from script. To change the value of script parameter simply assign new value to it.

mySwitch = 1

To get the value of script parameter simply refer to its name

print(mySwitch)

One reason to use setParameter for script parameter is if you want to call its callback function (if it has one). This doesn’t happen when you change the value of script parameter by assigning new value.

this:setParameter("mySwitch", 1)

But that’s inside script, how can I change mySwitch according to a button state on UI? Will the button send its state and thus change mySwitch if I create connection via drag-n-drop? I’ve tried it, but don’t see any change of mySwitch value.

My inquiry caused by a demand to hide\show and even move UI elements according to a certain values of variables used in the script. Is it achievable at all?

Once you connect a knob to parameter the knob will pick up the value of the parameter. Then when you move the knob it will change the parameter.

Yes, but use parameters for this. As you cannot connect script variables and macro page directly.

So a script variable will be relayed to a parameter via setParameter and UI button’s state will be passed to the parameter via connection drag-n-dropped on it? Am I correct?

Well, what I meant is use a parameter instead of variable in your script. In fact parameters and global variables are treated the same in the script. The difference is the state of global variables is not saved with the preset and also cannot be accessed from outside the script.

So if you have a variable xy in your script, define parameter with the same name and you can use it the same way as you would be using the variable. If you use the variable to store a number, define numeric parameter…

So basically the parameter is used as global variable.
https://developer.steinberg.help/display/HSD/Creating+Parameters#CreatingParameters-Parametersvs.GlobalVariables

Ok, no problem with using parameter instead of a variable. But I have issue now connecting parameter and UI element. Is it just a mere drag-n-drop to create a connection and have fully working pair “parameter - UI element”?

Yes, it should be. Most of the macro page controls and templates have value field. You can drop the parameter there if dropping on the canvas doesn’t work.

But you can also use right click menu if you prefer.

Ok, I’ve managed to connect an UI slider (with range from 0 to 127) and a parameter in the script, which defines a note number to play.

defineParameter("NoteSelector", nil, 0, 0, 127, 1) 

function onNote(event)
   local id = playNote(NoteSelector, event.velocity, -1, 1)
end 

The only issue now is that every time I reload the script the NoteSelector gets its default value (which is 0) and lowers down the related slider on UI. Why the parameter is not stored as it must be persistent?

It is stored - when you save the program.

You have 2 ways how to reload the script. One resets all parameters to their default values, the other remembers the parameter values.

Ok - two types of reload arrows are good. But when I add a new version of the script in the editor it will also drop down all parameters to thier default values, which is a bit annoying, when building an instrument.

If you change the script that’s to be expected.

It may be annoying for you when you build the instrument but the user will not be reloading the script. Unless there is some problem with it.

You can also choose your defaults. It doesn’t have to be 0. Or whatever is the min value of the parameter.

Yes, I understand that the the default can be something more convenient. But if I’m in the process of tweaking the instrument having all parameters reset to default would be really annoying.

I understand.

When you say you load different version of the script is it a different file?

Because if you make changes to the script in external editor and use the option to reload the script while remembering parameter values it should work. As long as it is the same file.

Getting back to the question of interaction between UI and Lua Script…

How can I execute a function (a block of code) in Lua Script with a switch on UI? I need something like this:

if mySwitch =1  then 
<block1>
else
<block2>
end

Your example looks correct, except for:

if mySwitch == 1 then

No,no - my main question is how a click on UI control can execute a function in Lua Script? For example I have connected a parameter in the script with a UI control (a button). Clicking the button changes the parameter. But how changing of a parameter can execute a certain function?

You need to define your parameter with a callback function.

function mySwitchChanged()
    if mySwitch == 1 then
        -- block of code
    else
        -- block of code
    end
end

defineParameter("mySwitch", nil, 0, 0, 1, 1, mySwitchChanged)

Thanks a lot! It worked. One little question remains. Why it all doesn’t work if I put defineParameter before the function mySwitchChanged()?

Because when you define the parameter the function doesn’t exist yet. But it is possible if you use anonymous function.

defineParameter("mySwitch", nil, 0, 0, 1, 1, function () mySwitchChanged() end)

If you do it like this you can define the callback function afterwards.