Introduction to Scripting In Kontakt, Part 3

In this 5 Part MPVHub Series, Toby Pitman explores the wonderful world of scripting in Native Instruments' Kontakt sampler. Part 3 goes deeper into variables. Read the next part on The Hub tomorrow!  

Last time we looked at some statements using if...else and select(). These helped us to run a script when something specific happened, like a note being played. If you remember we built a script that let us trigger (or un-bypass) some internal effects by pressing notes on our MIDI keyboard.

Part of this was done by targeting the Bypass control of the effects using some built-in variables, namely $ENGINE_PAR_EFFECT_BYPASS which was simply a reference to that parameter. In this part we're going to delve deeper into variables, both built-in and user defined.

What Is A Variable?

Variables are without doubt the most important thing in any scripting language and it would be quite hard to proceed with the rest of these workshops without explaining them in further detail. 

A variable is a storage device that holds a piece of information. At any point you can refer to that piece of information by referencing that variables name. Think of it like a shortcut.

You can fill a variable with a static (constant) piece of information like a name, number or an object. Alternatively you can fill it with something dynamic, like the current value of a changing parameter which can then be passed on to something else.

There are two main types of variable. Built-In and User Defined. Let's have a look!

Built-In Variables

The Kontakt Scripting Language has many built in variables which are written into the language. These are mainly references to the accessible parameters of the Kontakt engine, namely things you can manipulate with the KSP. Here's a good example. The Lo-Fi effect has four parameters. I can access these by using the built in variables for them. These are:


You'll notice that these all have a certain prefix of $ENGINE_PAR_. This prefix is reserved for Kontakt use only, as are $NI_, $CONTROL_PAR_, $EVENT_PAR_. When you come to define your own variables you'll want to steer clear of using these.

There are also other types that hold information about user actions, like what notes you're playing. We saw one of these last time, $EVENT_NOTE.

There are far too many to cover here but you'll find a list of all built in constants and Variables at the end of the KSP Reference Manual. Saying that I'll give you an example.

There is a built-in variable called $PLAYED_VOICES_INST. This holds the value of how many voices (notes) the current instrument is playing. If I write this on note callback...

Code example 1

I get this message.

You are playing 6 notes

You'll notice I've added one to the variable as it counts from 0. The ampersands (&) allow me to include the result between some written text (within quotation marks).

So you'll be using built-in variables to gather information about what is coming into Kontakt as well as accessing parameters and built in features within the Kontakt engine.

User Defined Variables

User variables are defined by you and because Kontakt is not familiar with these variables (unlike the built-in ones) you'll first need to declare them. Declaring a variable has to be done between your on init...end on callback. So what's it all about?

Declare is a way of saying, 'Hey Kontakt! I'm going to make a new variable, so listen up!'

For the past 20 years Toby has worked as a professional guitarist, programmer and producer. Clients include Sir Paul McCartney, George Michael, Shirley Bassey, Yusuf Islam, Giles Martin as well as the London 2012 Olympic Ceremonies. He has also worked extensively in TV, Advertising and Film. As well as composing himself he has also ... Read More


Want to join the discussion?

Create an account or login to get started!