Variables, Constants, and Arrays
What are Variables and Constants
Variables and constants are items you create and assign values to, to later use in your script. You can kind of think of them like containers. You make a container and give it a name. Then you put something in that container, maybe it’s a number, maybe it’s a line of text. That contain now keeps a hold of that item for you and remembers what is in it. You may be able to then update that container later, adding more or taking away from it, or maybe that container will always have the same item forever. Whatever their use, they remember values so you don’t have to and that is a huge help when making your script adaptable and flexible.
Variables
Variables, by their name and nature, are containers where the contents of that contain can vary. If it is a variable that contains a number, for example, you could add to that number or subtract from that number, or assign a completely new number to override the last one. A variable can be assigned a value at the start of the script, but that isn’t the end of it. It can then be changed later to suit the need of a script.
Note: Variables are often written in lower case (eg. $volgrp1) or camel case ($VolGrp1) as a fast way of identifying them as variables over constants.
Constants
A constant, as the name suggests, is a fixed value. Your container will be applied a value upon creation and then be fixed from that moment onwards. This is handy if you need to set a value that remains the same throughout the script.
Note: Constants are often written in upper case (eg. $SCRIPTNUMBER) as a fast way of identifying them as constants.
Common Types of Variables and Constants
Integer Variable ($)
The integer variable is one of the most commonly used variables in Kontakt scripting. This type of variable is denoted by a $ symbol and is expected an integer value, or a whole number value. The value can be negative or positive, but it can’t be a decimal or fraction (eg. 2.3 cannot be used as a value of an integer variable).
The most common place you will see an integer variable is with UI controls. For example, when declaring a UI slider, part of the process is declaring an integer variable name that will be attached to that UI slider and store the integer value that the slider is outputting (a possible 0 to 1,000,000 range). In the example below, a slider is declared and the integer variable $Vol is created along with it.
on init {Creating the performance view and UI dimensions.} make_perfview set_ui_height_px(300) set_ui_width_px(700) {Declaring the slider.} declare ui_slider $Vol (0, 1000000) end on
You will also see integer variables declared to be used as counters. These variables aren’t attached to a UI control, they are simply integer containers that can be updated throughout the script.
In the example below, an integer variable $i has been declared to be used as a counter. The counter is then used in a while loop to help progress the loop and also cycle through the groups.
on init {Creating the performance view and UI dimensions.} make_perfview set_ui_height_px(300) set_ui_width_px(700) {Declaring the slider.} declare ui_slider $Vol (0, 1000000) declare $i end on on ui_control ($Vol) {Reset $i to 0} $i := o {While Loop} while ($i < $NUM_GROUPS) {Set to go through each group.} set_engine_par ($ENGINE_PAR_VOLUME, $Vol, $i, -1, -1) {$i used to denote group.} inc ($i) {1 added to $i to increase the number before the next loop.} end while end on
You will also find many different integer variables (or constants) built in to Kontakt for use with its controls and parameters. Below are two examples. The Engine Par Volume is used, which is a Kontakt created variable, and the CC Num is used, which is also a Kontakt created variable.
on controller if ($CC_NUM=11) {Check if CC#11 is being moved.} $Vol := %CC[11] * 7874 {Assign value to $Vol based on controller.} set_engine_par($ENGINE_PAR_VOLUME, $Vol, 0, -1, -1) {Set Volume.} end if end on
Real Variable (~)
The real variable is similar to the integer variable, but it accepts “real” numbers, ie. numbers that can include decimal points. Some elements like XY Pads can use real numbers. In those situations, you may be able to use a real variable to store a value. They work exactly like integer variables, but are denoted with a ~ symbol.
Below is an example of a real variable being created and assigned a value. This variable would be useful with XY Pads, where the X Start Position variable could used later to assign the starting position of the XY Pad’s cursor along the X axis.
on init declare ~X_Start_Pos ~X_Start_Pos := 0.5 end on
String Variable (@)
A string variable is used to store a line of text, otherwise known as a string. This string can then be recalled or changed at any time. It is denoted by an @ symbol.
For example, below a string variable has been created and then used to provide a knob’s label with a title. Make sure you use “quotations” when typing in a string. These won’t appear on the label, but they are used to tell Kontakt that everything inside these quotations is a string.
on init declare @Name @Name := "Volume" declare ui_knob $Vol (0, 629000, 1) set_text($Vol, @Name) end on
This could be useful when changing the function of a knob based on the value of a switch. Let’s say, for example, you want to knob to control Volume when the switch is off and the Cutoff when the switch is on. You could update the variable name based on the switch’s position.
on init declare @Name @Name := "Volume" declare ui_knob $Knob (0, 629000, 1) set_text($Knob, @Name) declare ui_switch $Sw $Sw := 0 end on on ui_control ($Knob) if ($Sw = 0) @Name := "Volume" set_text ($Knob, @Name) else @Name := "Cutoff" set_text ($Knob, @Name) end if end on
Integer and Real Constants
Constants, as discussed above, are unchanging values and are usually denoted with a $ symbol for integer values and a ~ symbol for real values. String variables can’t be turned into constants, however, there is nothing stopping you from treating a variable like a constant, by simply never updating the value of a variable elsewhere in the script. Constants are also typically denoted through upper case names to show they are constants and if you are using a variable like a constant it may be a good idea to do the same.
Below are simple declarations of a constants, one an integer constant and one a real constant. The difference between variables and constants is that a constant must be assigned a value in line with its declaration (this can be done with a variable, though often it is left until later). So, you will notice the value is assigned straight after the declaration.
on init declare const $VALUE := 123 declare const ~NUMBER := 12.3 end on
What Are Arrays?
Arrays are containers that can hold more than one value. It’s kind of like a filing cabinet, there are many different files inside the cabinet and they are all organised by number, alphabet or another organisation system. Arrays can be useful when you want to store a number of values at once and they recall values after. Arrays can store integer, real or string values, based on the type of array you declare.
Integer Arrays (%)
Integer arrays store integer (whole note) values. For example, you may want to store an array that contains 3 note values, 60, 72 and 84 (C3, C4 and C5). You need to declare an integer array, set a name and decide on how many elements can be stored in that array, all before you can assign these values.
Below, we can see the %Note_Arr being declared and the [3] denotes the number of elements we need (eg. 3). If you required 25 elements, you would need to use a [25] instead, for example. The values are then being assigned in the brackets, using commas to separate between each value. It is important you list every element’s value if they are all to be different.
on init declare %Note_Arr[3] := (60, 72, 84) end on
You can also assign values after declaring the array. Below, each element is assigned a value independently. Just remember, while declaring the array you set the number of elements as 3, but when counting through the elements at any other time in the script, they will be elements 0, 1 and 2, because of the zero-based computer counting system.
on init declare %Note_Arr[3] %Note_Arr[0] := 60 {First Element.} %Note_Arr[1] := 72 {Second Element.} %Note_Arr[2] := 84 {Third Element.} end on
All elements in an array can be set to the same number by simply assigning only one value to all arrays when declaring the array. This could be helpful if you are going to be updating and changing these variables regularly. In the below example, an array of 10 elements have been created and all assigned the value of 0, but then element 8 has been assigned a value of 1 later.
on init declare %Note_Arr[10] := (0) %Note_Arr[7] := 1 {Assigning the 8th Element. } end on
There are examples of built in integer arrays in Kontakt. For example, the MIDI CC Chanel values are stored in the MIDI CC Array. There are 128 elements in the array, each representing the 128 (0-127) possible MIDI CC channels, and each element is updated with the current value outputted by the MIDI Controller.
For example, below the MIDI CC Chanel 11 (Expression) is used to update a variable and move the volume control of the first group’s volume. You don’t need to declare the Array, as it is already there, but the array can be used and updated.
on controller if ($CC_NUM=11) {Check if CC#11 is being moved.} $Vol := %CC[11] * 7874 {Assign value to $Vol based on controller.} set_engine_par($ENGINE_PAR_VOLUME, $Vol, 0, -1, -1) {Set Volume.} end if end on
Real Arrays (?)
Real arrays work in the same way that an integer array works, however, they now deal with real numbers. Below is an example of a real array. It is almost identical as the integer array, though this time it can store numbers with decimal points.
on init declare ?Real_Arr[2] := (0.5, 1.0) end on
Some UI controls are real arrays. The XY Pad, for example, is a real array that creates cursors based on the number of elements in the array. In the example below, an XY pad is created with two elements (an X position and a Y position for the first cursor). A starting value is then given to both X and Y to place the cursor in the middle position of the pad. The maximum value of the X or Y position would be 1.0, so 0.5 is half way.
on init declare ui_xy ?XY_Pad[2] ?XY_Pad[0] := 0.5 ?XY_Pad[1] := 0.5 end on
String Arrays (!)
String arrays work in the same way, only they store string values instead of real or integer number values. “Quotations” are used again to denote a string value. Unlike real and integer arrays, where the values can be assigned in line with the declaration, you need to assign values to the elements after the array has been declared.
In the example below, a simple string array has been created for labels, to store two different string values.
on init declare !Label_Arr[2] !Label_Arr[0] := "Volume" !Label_Arr[1] := "Cutoff" end on
This array can then be used to call back values at a later time. For example, similar to a previous switch example for string variables above, now we can enhance it by using an array. This helps collapse the code lines in the IF function and makes the code more adaptable.
on init declare !Label_Arr[2] !Label_Arr[0] := "Volume" !Label_Arr[1] := "Cutoff" declare ui_knob $Knob (0, 629000, 1) set_text($Knob, !Label_Arr[0]) declare ui_switch $Sw $Sw := 0 end on on ui_control ($Knob) if ($Sw = 0) set_text ($Knob, !Label_Arr[0]) else set_text ($Knob, !Label_Arr[1]) end if end on