Last modifiedby & filed under KSP Scripting (NI Kontakt) - Tutorials, KSP: Basics.

The Basic code structure is very simple. We basicaly put most of our code into  callbacks

All callbacks look like this on <callback> ... end on

In general the code inside callbacks gets executed when specific events happen. For example playing a note on our keyboard always executes the “on note” callback.

So callbacks build a framework for our instrument and are mostly needed to communicate with different parts of the Kontakt engine.

Imagine it like that: an event triggers the related callback which is executing all our code inside it. Thereby sometimes the same callback can be called multiple times. For example playing 2 notes on our keyboard will execute 2 instances of the on note ... end on callback.

There are more callbacks as introduced in this article but with the ones introduced here we can already create awesome instruments.

Note: some commands & constants can only be used inside specific callbacks. For example the  wait() & purge() command can’t be used inside the init callback.

on init […] end on

the on init callback gets executed automatically on every start of our instrument. It also gets executed when hitting the apply button, when loading snapshots or when restarting the Kontakt engine (panic button).

The init callback will be the starting point for most of our scripts. Because inside on init ... end on  we have to put in all the initial code (like variables, ui elements, etc) we are even only allowed to declare new variables, arrays and UI controls (knobs, buttons, menus,…) here.

So the init callback is probably the most used callback and part of most of our scripts.

Example: simple init

on init
   set_ui_height_px(540)
   make_perfview
   declare ui_switch $switch
   declare ui_knob $knob (0,1000000,100)
   make_persistent($knob)
   message ("")
end on

on note […] end on

this callback is executed when we play a note. Several instances can be called at once when we play multiple notes on our keyboard.

We can use it to trigger other events whenever playing a note (like led lights for our ui) or of course we can use it to manipulaute the played notes themselves.

For example we can create a round robin application for more realistic instruments or we can create simple intervals & chords, like in the example below.

Example (this will add two additional notes, creating a chord)

on note
   play_note($EVENT_NOTE+4,$EVENT_VELOCITY,0,-1)
   play_note($EVENT_NOTE+7,$EVENT_VELOCITY,0,-1)
end on

on release […] end on

same as on note but executed when releasing a key

on ui_control(<variable>) […] end on

gets executed when using the assigned UI element (knobs, buttons, switches, menus etc.)

To assign an ui element we simply have to put its variable name after on ui_control (but remember first declare the ui elements inside the on init).

We can assign any variable we want like this

on init
  make_perfview
  declare ui_button $MySuperButtonToControlTheWorld
end on


on ui_control ($MySuperButtonToControlTheWorld) 
  {our master piece of a code waiting to be executed when pushing the button}
end on

For each of our already declared controls we can add an individual on ui_controlWe can add as many on ui_control .. end on callbacks as we want.

Once we have assigned an ui element we can then use the on control callback to control almost anything we want with our custom ui (we will learn more about this later on).

(of course you don’t wanna use that long variable names unless you like wounded fingers)

In general we use the on ui_control to adjust engine parameters like FX settings (e.g. reverb time, attack time, filter cutoffs etc.). But we can also use it to change other settings like UI behaviors, or to activate other functions, to play a note, midi files and many more.

Example – this sets the attack and release time of an AHDSR, named “ENV_AHDSR”:

on ui_control ($attack)
   set_engine_par($ENGINE_PAR_ATTACK, $attack, $group, find_mod($group,"ENV_AHDSR"), -1)
end on

on ui_control ($release) 
  set_engine_par($ENGINE_PAR_RELEASE, $release, $group, find_mod($group,"ENV_AHDSR"), -1) 
end on

NOTE: You can find a full list of all callbacks in your KSP reference manual.

How useful was this article?

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

NOTE: highly negative votes may be removed without any reasonable given feedback!

Tell us how we can improve this post?

3 Responses to “KSP Scripting (NI Kontakt) :: BASICS :: Callbacks & Basic Structure”

  1. Serge

    Hello! To call a sound group on my instrument I use "purge_group()...". All is OK until I want to launch the sound from my keyboard controller button - all status changes are performed correctly, all indications are working fine, but no sound on output, because "on controller" callback doesn't allow any "purge_group()..." or "allow_group()...". The "on ui_control()..." doesn't react on changes that are performed. Could You help me to solve this problem - how to realize sound launch from controller button? Tested with different keyboards, the same problem every time - indications are working in proper way, but sound can't be launched. I use KONTAKT 6.0.3. Thanks in advance for Your reply. Best regards, 02/01/2019 Serge

    • YummyBeats

      hey, all I can think of right now is a workaround. If you train a slider with your preferred midi controller and save your nki, the assigned midi controller will be stored as well.
      So this is what you could do:
      1. Add a slider, make perfview
      2. exit edit mode
      3. learn midi automation with right mouse click on the slider (I used the Mod Wheel)
      4. save your nki
      5. close Kontakt and reopen your nki
      6. Hide your slider via $CONTROL_PAR_HIDE
      7. save your nki again
      Here is an example:
      on init    
      	declare ui_slider $button (0,1)
      	$button := 0
      	{set_control_par(get_ui_id($button),CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)} {learn midi automation => save nki => hide button-slider => save nki again}
      	make_perfview
      	message("")
      end on
      
      on ui_control($button)
      	if($button = 1)
      		purge_group(0, 1)
      		message("group 0: samples loaded")
      		else
      			purge_group(0, 0)
      			message("group 0: samples unloaded")
      	end if
      	wait(1000*1000*2)
      	message("")
      end on
      

      • Serge

        Hello! Thanks a lot for Your feedback. Your proposal has been tested today with both knobs & sliders versions, working fine. Especially useful, if some earlier programmed buttons can't be learnt by soft. NOTA => please, take into account, that while using the "select()..." function, several cases/situations can be appointed to the same one slider. Big thanks once again, wish You all the best! 03/01/2020 Serge.

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 *