Code Examples
This document offers a range of Lua code examples tailored for the Q-SYS platform. These examples are designed to help you automate tasks and create control mechanisms within your Q-SYS designs using Lua scripting.
 Passing an Input to the Output
Passing an Input to the Output
                The following is a simple script in which the input of the control that changed (changedControl) is passed to the output (Controls.Outputs[1]).
function UpdateControl ()
  Controls.OutputKnob.Value = Controls.InputKnob.Value
end 
Controls.InputKnob.EventHandler = UpdateControl Define a Function During Assignment
Define a Function During Assignment
                Lua supports anonymous functions so you can define the function during assignment as follows:
Controls.InputKnob.EventHandler = function(ctl)
  --Note: The EventHandler function passes the control in as a argument automatically.
  --In this example we named it ctl, but this variable can be any name. 
  Controls.OutputKnob.Value = ctl.Value
end
 Address an Input Directly
Address an Input Directly
                The signature of the Control value EventHandler takes the control that changed as an argument. Due to Lua's 'loose' typing, it is possible to remove that argument and just address the input directly.
Controls.InputKnob.EventHandler = function()
  Controls.OutputKnob.Value = Controls.InputKnob.Value
end Determine System Mode - Emulate or Not
Determine System Mode - Emulate or Not
                if System.IsEmulating then
Controls.Outputs[1].Color = "Green"
else
Controls.Outputs[1].Color = "Red"
end Performing Math
Performing Math
                Controls.InputKnob.EventHandler = function()
  Controls.OutputKnob.Value = Controls.InputKnob.Value / 2
end Vector Value Controls
Vector Value Controls
                Note: This operation is only possible in a Control Script.
--To access the vector output of a meter. 
 
t = Controls.Inputs[1].Values
print( t[1]..","..t[2]..","..t[3]..","..t[4] ) Multiple Inputs
Multiple Inputs
                If you have multiple inputs and want to run the same script if any of the inputs change, the easiest way to accomplish this would be to iterate all the inputs.
function ButtonPressed ()
  print("One of the Buttons Was Pressed")
end 
for idx,ctl in ipairs(Controls.MultipleButtons) do
  ctl.EventHandler = ButtonPressed
end Ramp or Position Controls
Ramp or Position Controls
                You can also ramp control values or positions via script. RampTime is specified in seconds.
Note: Once a RampTime has been assigned to a Control, it will keep this RampTime until it is changed or the script is restarted.
Controls.InputKnob.EventHandler = function()
  Controls.OutputKnob.RampTime = 3
  Controls.OutputKnob.Value = Controls.InputKnob.Value
end Metadata
Metadata 
                When controls have metadata attached to them, you can do the following:
- set a control to visible or invisible,
- set a control to enabled or disabled,
- specify its choices, and
- set its color.
Metadata is used on some built-in controls - for example, the High-Pass Filter uses metadata to disable the Q control when the filter type is not set to Variable Q. The controls in the Custom Controls component have metadata turned on by default. Attempting to access metadata on a control which does not have metadata will result in an error.
Color metadata is in the form of a string. Q-SYS uses the built in .NET string->color converter which allows for strings in the form "#RGB", "#RRGGBB", CSS Color Names, and HSV values using the format "!HHSSVV".
 NamedControl
NamedControl
                Note: See the NamedControl section for syntax.
NamedControl.SetValue("CustomControlsInteger1", 7, 15)  -- Set value of a named integer control to 7, ramp 15 seconds
NamedControl.GetString("CustomControlsTextdisplay1")  --Get string value of a control
NamedControl.Trigger("CustomControlsTrigger1")  --Triggers a controlled trigger button Timer
Timer 
                Each Control Script has a timer available. It has a Tick event that a TickHandler can be attached to. You can start or stop the timer. The Timer TickHandler signature looks like:
Note: See the Timer section for syntax
--Simple Timer
Controls.InputKnob.EventHandler = function()
  Timer.CallAfter(
    function()
      Controls.OutputKnob.Value = Controls.InputKnob.Value
      end, 3 --Time in Seconds to Wait
  )
end
--Named Timer
myTimer = Timer.New()   --Define timer
myTimer.EventHandler = function()   --Event to run when timer expires
  Controls.OutputKnob.Value = Controls.OutputKnob.Value + 1
end
Controls.UpButton.EventHandler = function(ctl)
  if ctl.Boolean then  --if button is on / pressed
    myTimer:Start(.5) --Argument is number of seconds to run the timer
  else -- if button is off / released
    myTimer:Stop()  --Note: Timer will keep repeating until stopped
  end
end