The Basic code structure is very simple. There are several callbacks where you basically put in your code. At least you need the init callback
on init ... end on The code inside callbacks gets executed when specific events happen. For example playing a note on your keyboard always executes the “on note” callback.
You can imagine it like that: an event triggers the related callback which is executing all your code inside it. Thereby the same callback can be called multiple times. For example playing 2 notes on your keyboard will execute 2 instances of the
on note ... end on callback.
There a re more callbacks like the ones covered here but with the here introduced callbacks you can already create awesome instruments.
Note: some internal commands & constants can only be used inside specific callbacks, like e.g. the
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 ans egen restarting the Kontakt engine (panic button). On init is always required because you can only declare new variables or UI controls (knobs, buttons, menus,…) here. You also have to use it for defining all the default or initial values.
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 you play a note. Several instances can be called at once. For example you can use it to create a round robin playback for more realistic instruments or like in the example below to create intervals & chords.
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
executed when using the related UI elements (knobs, buttons, switches, menus etc.) For each of your controls you can add an individual
on ui_control and asign a custom variable which you can then control or use to control other parameters. In the first place we use the on ui_control to change engine parameters like FX settings (e.g. reverb time, attack time, filter cutoffs etc.). But we can also use them 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.