How to Write Comments in Your Kontakt Script
What are Comments?
Many scripting languages have the ability to add comments to your code. You often need comments in order to quickly jot down something you need to remember. Maybe you want to add a comment that lets you know what that variable controls? How about next to the knob you just created to remind you of what it is attached to? They can also be incredibly handy when formatting your code. It is quite common in many scripting languages to use the tab key to indent lines that are nested inside functions, such as the contents of a callback (see this article). However, you may want to divide up your script with useful titles, so the ability to add comments to create these titles would be very handy indeed.
The problem is you can’t just type out a sentence in your code, “this knob controls reverb”. If you were to do that, Kontakt would be looking at that string of characters and assuming it is some kind of function, at which point it would try and process that function and then throw up an error because it doesn’t exist (surprise, surprise). We need to tell Kontakt that this string of characters shouldn’t be read as code and should instead just be ignored. That is why being able to bracket something and label it as a comment is so useful.
All comments in Kontakt are wrapped in these: {}. Whenever Kontakt sees something inside these curly brackets, it just ignores the text knowing that it is purely there for the humans who will be reading through the code, debugging it or trying to remember what they have done. So if I write {Hello World} in this way, Kontakt will skip over “Hello World” and not try to run it as a command. Comments don’t have to just be on a single line either, they can be spread over a few lines, as long as the start of the comment begins with a { and ends with a }. Below are some examples of common uses of comments and some best practices.
Commenting In-Line With Your Code
A very common practice in coding is to use comments every time you do something new, to let your future self or anyone else who may read the code know why you do something in that particular way. In the example below, a knob has been declared, so a comment has been used to describe its purpose.
on init declare ui_knob $LPCut (0, 1000000, 1). {Declaring the Low Pass Filter Cutoff Knob and setting the min and max values.} end on
Commenting Titles In Your Code
If you want to separate a section of code with titles, you can do so through comments. In the example below, comments have been used to create two separate titles, one for the Variables and one for the Constants (see this article). They are spread across multiple lines, but start and end with {}. Asterisks have been used to help with visibility (a common practice is to use a -, # or * to make the section easily identifiable when scrolling quickly through the code). In-line commenting has also been used still to define the purpose of each variable or array.
on init {******************************** Declaring the Variables. ********************************} declare $i := 0. {Create a counter and assign a value of 0.} declare ui_slider $Vol (0, 600,000). {Create a Volume Slider with a range of -inf to 0db.} {******************************** Declaring the Constants. ********************************} declare const $NUM_GROUPS := 12 {Declare a constant for the number of instrument groups.} end on
Commenting Your Script Title
When working with an external script editor (which is definitely preferable to the Kontakt Script Editor window when working on larger scripts) it is often common practice to create a script title at the top in order to verify which script version is currently copied across to the Kontakt Instrument. Again, as above, this is made obvious through use of symbols (often the # key) and is bracketed with {} at the start and end of the title. Inside the box this creates, the title of the script, version and author may be included, as well as any other important details worth noting.
{################################################################ Script Title: “Kontakt Instrument Script.ksp” Script Version: v1.3 Script Author: John Smith ################################################################}
Commenting Out Code
It is also quite common to “deactivate” code by commenting it out. This basically means you surround the line you want to deactivate with {} and that now becomes a comment, meaning Kontakt will no longer action that command. This can be useful if you want to try a different function or command, but you are not ready to remove the old one yet. It is also useful in debugging, allowing you to isolate and deactivate a section of code and see if the removal of that code removes the problem. In the example below, one line of code is commented out in favour of testing another.
on ui_control ($LPCut) {set_engine_par ($ENGINE_PAR_ATTACK,$LPCut,-1,0,1)} {Commented out line.} set_engine_par ($ENGINE_PAR_CUTOFF,$LPCut,-1,0,1) end on