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 we can store values and read or access them again anywhere we want throughout the script.So they are aromatically global.

There are different variable types: string, integer and constant variables.

Furthermore there’s a special polyphonic variable which is not addressed in this article.

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 we can store complete sentences! And yes, she works fine :)"
     message(@hint)
end on

Constant Variables

The values of constants can’t be changed anymore once they have been set. You may wanna use them to avoid accidentally 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 on:

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

This works

on init
    declare const $MY_CONST := 10
end on

This won’t work

on init
    declare const $MY_CONST
    $MY_CONST := 10
end on

This won’t work either

on init
    declare const $MY_CONST := 10
    $MY_CONST := 11
end on

Internal Variables

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

There are also internal variables which are holding specific values returned from the Kontakt engine like the actual played note ($EVENT_NOTE) or engine parameters, but also UI parameters and many more.

We use them to control or to address all kinds of Kontakt internal engine parameters like the Instrument’s Volume, or our ui controls and many other parameters. There is a full list of all internal variables and what they do in the 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.

Working with these Variables is mainly what Kontakt scripting is about so you will see them a lot during later tutorials. And you will get familiar with their usage and with looking them up, very quickly. At best study all of them to get to know what Kontakt is capable of and what not. Also this may inspire you.


ARRAYS

We can also store values into arrays. Compared to a usual variable an array holds multiple values each one assigned to a different key inside the array. Think of it like a big filing cabinet with many drawers. Each drawer (key) holds an individual value. We can access and read those values again by calling the array giving the specific key.

To do so we declare an array, specify its size (number of drawers/keys) and fill each single key with our values (like shown below).

There are also int and string arrays (see below).

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)

Leave a Reply

use <pre></pre> to wrap code blocks

use <code></code> to wrap small code snippets

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