Posted by & filed under KSP Scripting (NI Kontakt) - Tutorials, KSP: Basics.

Like in other programming languages in KSP there are variables, arrays and constants. These can only be declared inside the on init callback but can be used throughout the whole script then.

Variables

Into variables you can store values and read or access them again anywhere you want. There are different variable types: string, integer and constant variables. Furthermore there’s a special polyphonic variable which is not addressed here.

Integer Variables

can only store integers (numbers) and have to be declared like this declare $<variable-name>  the $ makes it an int variable

on init
     declare $count := 8 {sets the variable named "count" and stores 8}
     message ($count) {outputs 8}
 end on

String Variables

can only store strings and have to be declared like thisdeclare @<variable-name> The @ makes it a string variable

on init
     declare @hint:= "Within string variables you can store complete sentences!"
     message(@hint)
end on

Constant Variables

Constants can’t be changed anymore once they have been set. You may wanna use them to avoid accidently overwriting them especially when your code gets more complex. User defined constants can be declared by adding const before the variable . You also have to define the value right after like this:

declare const $<variable-name>:= <value>

Internal Variables (also called Built-In Variables, Control Parameter Variables, Engine Parameter Variables)

The internal variables are containing specific values from Kontakt like the actual played note ($EVENT_NOTE) or engine parameters, but also UI parameters and many more. You can use them to control or to address all kinds of Kontakt internal engine parameters like the Instrument’s Volume for example, or your ui controls position and any other parameter you can imagine. You can see a full list of all internal variables and what they do in your KSP Reference Manual. Without these variables we wouldn’t be able to operate Kontakt with custom scripts. Internal variables cannot be declared or changed they are always available globally. Reading and using these Variables is mainly what Kontakt scripting is about so you will find several examples during later tutorials and you will get familiar with many of them very quickly.

Arrays

You can also store values into arrays. Compared to a usual variable an array holds multiple values each assigned to a different key inside the array. You can imagine it like a big cupboard with many drawers. Each drawer (key) contains a value. You can read those values again by calling the array and giving the specific key. To do so declare an array, specify the size of the array (number of drawers/keys) and fill each single key with your values like shown below. There are also int and string arrays.

int array

on init
    declare %parameter[4] {% defines an int array named "parameter", between [] you define the amount of available keys}
    %parameter[0] := 10 {first key index starts with 0}
    %parameter[1] := 126
    %parameter[2] := 10024
    %parameter[3] := 8
    message(%parameter[2]) {outputs 10024}
end on

string array

on init
     declare !notes[4]
     !notes[0] := "C"
     !notes[1] := "C#"
     !notes[2] := "D"
     !notes[3] := "D#"
     message(!notes[3]) {outputs D#}
end on
 

2 Responses to “KSP Scripting (NI Kontakt) – BASICS :: Variables, Arrays, Constants”

  1. Serge

    Hi _

    Fot the @ example, I would have written it like this : she work fine .

    declare @hint
    @hint := “Within string variables you can store complete sentences!”
    message (@hint)

    reply

Leave a Reply

Your email address will not be published. Required fields are marked *