Posted by & filed under KSP Scripting (NI Kontakt) - Tutorials, KSP: Sublime 3.

You get Sublime 3 from https://www.sublimetext.com/3

Note: you can use it without any restrictions but you still need to buy a license. It’s really worth it!

Thx to Nils Liberg and nojanath who created a KSP plugin for Sublime Text with many new possibilities like macros, for statements, functions and many more, using Sublime is almost essential when it comes to complex scripts since it simplifies things a lot (like structuring our code and saving lots of code-lines). It also helps us to better reveal some errors. There are also new practices & features, like a different (easier) coding with less required code.

Example – less code writing

we can write this line:

set_control_par_str(get_ui_id($textfield),$CONTROL_PAR_TEXT,"hello, it's me")

like this now (the above is still allowed):

$textfield -> text: "hello, it's me"


Also refer to this guide:
https://github.com/nojanath/SublimeKSP/wiki/Added-Features


CONTENT

 

 


Installation

  1. download Sublime 3 and install it (at best as portable version on a non-system drive)
  2. install Package Control (installation instructions)
  3. restart Sublime
  4. install the special KSP Plugin from Nils Liberg / nojanath fork (GitHub) by opening the command palette with:
    Command Shift P
    Ctrl Shift P
  5. type “install package”
  6. type “ksp” and select “KSP (Kontakt Script Processor)”
  7. hit    ↵     to install
  8. restart sublime

Start new project – activate KSP syntax

if you open a new tab, start writing and your code stays just white, the KSP syntax is not activated.

 

  1. To activate the KSP Syntax hit:
    Command Shift P

    Ctrl Shift P
  2. type “ksp” and select “Set Syntax: KSP”
  3. copy & paste the following test code into your tab. If it’s colorized now everything works fine:
on init
    declare ui_label $lbl(1, 1)
    make_perfview
    set_ui_height_px(300)
    set_ui_color(9aaaaaah)

    $lbl -> text := ""
    $lbl -> text := 'WAITING FOR COMMAND'
    $lbl -> width  := 300
    $lbl -> font_type := 5
    $lbl -> textpos_y := 2
    $lbl ->text_alignment := 1
    $lbl -> pos_x := (632-300)/2
    $lbl -> pos_y := 200

    declare $btn_width := 100
    declare $btn_height := 28
       declare $center_x := (632-$btn_width)/2

       declare $output

       //declare_button(#idx#,#name#,#x#,#y#,#pos_x#,#pos_y#)
    declare_button(0,main,$btn_width,$btn_height,$center_x,50)
    declare_button(1,main,$btn_width,$btn_height,$center_x,100)
end on

on_ui_control_button(0,main)
on_ui_control_button(1,main)


macro declare_button(#idx#,#name#,#x#,#y#,#pos_x#,#pos_y#)
    declare ui_button $btn_#name#_#idx#
    $btn_#name#_#idx# -> text := "BUTTON " & #idx#
    $btn_#name#_#idx# -> width := #x#
    $btn_#name#_#idx# -> height := #y#
    $btn_#name#_#idx# -> pos_x := #pos_x#
    $btn_#name#_#idx# -> pos_y := #pos_y#
end macro

macro on_ui_control_button(#idx#,#name#)
    on ui_control($btn_#name#_#idx#)
        myFunction(output, #idx#)
        $btn_#name#_#idx# := 0
    end on
end macro

function myFunction(output, idx)
    $lbl-> text := "BUTTON" & idx & ": CALLED ME"
    message("BUTTON " & idx & ": CALLED ME")
end function

Sublime Settings

here are some recommended settings you should do when it comes to compiling your code. Go to tools and enable all KSP settings as shown in the screenshot:

Explanation:

  • Remove Indents: when compiling all indents are removed which slightly compacts  your code
  • Compact Variables: this compacts your code by shorten all variable names.
    Note that this makes your compiled code almost unreadable 

    Original Code

    on init 
         set_ui_height_px(540) {height of your UI}
         make_perfview {makes this UI visible outside the edit mode}
     
         {SET: initial wallpaper}
         set_control_par_str($INST_WALLPAPER_ID,$CONTROL_PAR_PICTURE,"wallpaper1") {sets your initial wallpaper which can only be overlayed}
     
         {SET: vraiables}
         declare $label_id {used to store the label id, later we will use arrays}
         declare $button_id {used to store the button id, later we will use arrays}
     
         {SET: label / wallpaper 2}
             declare ui_label $wallpaper (0,0) {creates a label "container" for your additional wallpapers}
             $label_id := get_ui_id ($wallpaper) {get the numeric unique id of the label}
     
             set_control_par_str($label_id ,$CONTROL_PAR_TEXT,"") {clear the default text}
             set_control_par_str($label_id ,$CONTROL_PAR_PICTURE,"wallpaper2") {set background image}
             set_control_par($label_id ,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL) {hides the whole label because we first use the initial wallpaper}
             move_control_px($wallpaper,0,0) {move to top left position}
     
         {SET:button}
             declare ui_switch $button_switch_wallpaper {create a simple button to switch the wallpapers}
             $button_id := get_ui_id ($button_switch_wallpaper) {get the numeric unique id of the button}
        message("") {clears the status bar}
    end on
     
    on ui_control($button_switch_wallpaper)
         if ($button_switch_wallpaper = 1)
             set_control_par($label_id ,$CONTROL_PAR_HIDE,$HIDE_PART_NOTHING) {shows your label}
             else
                 set_control_par($label_id ,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
         end if  
         message("") {clears the status bar}
    end on

    Compact Code:

    { Compiled on Fri Mar 10 164:13:29 2017 }
    on init
      declare $yjjle
      declare $q3qgi
      declare $t0rma
      declare $vlhs0
      set_ui_height_px(540)
      make_perfview
      set_control_par_str($INST_WALLPAPER_ID,$CONTROL_PAR_PICTURE,"wallpaper1")
      declare $s1cn2
      declare $hr102
      declare ui_label $rhngx(0, 0) 
      $s1cn2 := get_ui_id($rhngx)
      set_control_par_str($s1cn2,$CONTROL_PAR_TEXT,"")
      set_control_par_str($s1cn2,$CONTROL_PAR_PICTURE,"wallpaper2")
      set_control_par($s1cn2,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
      move_control_px($rhngx,0,0)
      declare ui_switch $5bcxz
      $hr102 := get_ui_id($5bcxz)
      message("")
    end on
    
    on ui_control($5bcxz)
      if ($5bcxz=1)
        set_control_par($s1cn2,$CONTROL_PAR_HIDE,$HIDE_PART_NOTHING)
      else
        set_control_par($s1cn2,$CONTROL_PAR_HIDE,$HIDE_WHOLE_CONTROL)
      end if
      message("")
    end on
  • Extra Syntax Checks: this detects some additonal syntax errors. (Undeclared variables, wrong statements are already checked by default)
  • Signal Error on Empty if/case statements: simply warns you when you leave statements empty like this
    if (1=1)
             
    end if
  • Optimize Compiled Code: this simply optimizes your code

Compiling

You have to compile your code when you are using all the helpful extra features since this one $textfield -> text: "hello, it's me" wouldn’t work at all if you copy it into Kontakt.

When compiling Sublime 3 “rewrites” your code again and makes it readable for Kontakt. So the above gets again set_control_par_str(get_ui_id($textfield),$CONTROL_PAR_TEXT,"hello, it's me")

But actually you don’t have to worry about this. Simply compile your code by hitting   F5    . The compiled code gets copied to your clipboard and you can simply paste it into Kontakt  Ctrl V
If there are some errors in your syntax Sublime aborts compiling and outputs a warning pointing you to the affected line.

You can use the above test code and try to compile it.

 

Leave a Reply

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