# Nils Liberg's Kontakt scripting tools



## Thonex (Jan 26, 2006)

Hi Geeks,

Here is a useful tool to help in K2 scripting. It's free and it does a great job in keeping things clean and organized:

http://nilsliberg.se/ksp/



> Nils Liberg's KScript Editor (version 0.94):
> This is an editor for Kontakt2 scripts that's much more powerful than the internal Kontakt editor. Features:
> 
> * Syntax highlighting of script code
> ...



Hope you find it useful.

T


----------



## jamriding (Apr 15, 2006)

As I said in another reply, I don't have Kontakt yet but thanks very much for offering it to all members.

Much appreciated. :smile:


----------



## Hans Adamson (Apr 15, 2006)

I downloaded it. Now I just have to learn how to use it. :???:


----------



## Thonex (Apr 15, 2006)

Hans Adamson @ Sat Apr 15 said:


> I downloaded it. Now I just have to learn how to use it. :???:



There's not too much to learn. If you want, you can just use it as an editor... and use the indent feature to keep things nice and clean. It aslo makes bold most of the K2 commands so you can see what your doing.

If you want, you can learn how to use it's functions features etc....

It's pretty much a must have if you want to make K2 scripting easier.

T


----------



## frankvg (Apr 16, 2006)

Make sure you have the latest version: 0.97

cheers,
Frank


----------



## kotori (May 9, 2006)

Hi everyone,
there's a new version of the http://nilsliberg.se/ksp/setup-kscript-editor-0.98RC4.exe (Script Editor - 0.98 RC4) (only PC version so far). It has not yet reached the status of an official 0.98 version, but it seems pretty stable. Feel free to try this out and let me know if you have any problems. When I have confirmed that everything works as it should I'll release an official 0.98 version and try to ask Frank to help us with an OSX port (if he has the time).

New features / bug fixes:
Support for rpn/nrpn callbacks
Support for all new variables and functions in Kontakt 2.1
Variables declared inside functions can now be both local (default) or global. See below.
 'Copy as BBCode' function for posting syntax highlighted code on forums. Copying and pasting such code back into the editor also works. (see the code below for a sample)
Fixed bug in auto indent - for-loops weren't properly indented.
It's possible to wrap strings inside ' instead of ". The former works better with the syntax highlighting.
New type of comment. Everything written inside (* *) is removed from the compiled version. Good for commenting out unused code as it's possible comment out comments as well.
Here's an example of the new 'global' keyword. If you prefix it to a variable declaration inside a function that variable becomes global. Without the prefix the variable is local to the function (if two functions declare local variables with the same name these won't interfer with each other).

``*function* foo
````*declare* x`````````_{ local variable }_
````*declare* global y``_{ global variable (accessible from other functions/callbacks) }_
``*end function*

You should only use the 'global' keyword for variable declarations inside functions. Declarations inside 'on init' should use the standard KSP syntax.

Cheers,
Nils

_Edit: did a minor bug fix and changed the link above to point to RC3_


----------



## Nickie Fønshauge (Jun 16, 2006)

Nils, it seems to me there is a slight problem with your notion of "local" variable. Apparently local variables are declared for each definition of a function, instead of for each call to the function. That way they are not really local. The way I understand "local" is, that the variable is local at runtime.

If I f.ex. define a function _func_ with a local variable _x_ and a global variable _y_, and call _func_ from two different controller callbacks, _x_ turns up as __x_ and _y_ as _y_ in both calls to _func_. Not a lucky situation, if both calls occur at the same time. It would be better, if _x_ was declared as distinct variables __xn_, one for each call to _func_.

Can you confirm, that it works this way?


----------



## TheoKrueger (Jun 16, 2006)

Thanks so much Nils. Your tools have made it easier for people to make great scripts.


----------



## kotori (Jun 16, 2006)

Thanks Theo! :smile: 



Nickie Fønshauge @ Fri Jun 16 said:


> Nils, it seems to me there is a slight problem with your notion of "local" variable. Apparently local variables are declared for each definition of a function, instead of for each call to the function. That way they are not really local. The way I understand "local" is, that the variable is local at runtime.
> 
> If I f.ex. define a function _func_ with a local variable _x_ and a global variable _y_, and call _func_ from two different controller callbacks, _x_ turns up as __x_ and _y_ as _y_ in both calls to _func_. Not a lucky situation, if both calls occur at the same time. It would be better, if _x_ was declared as distinct variables __xn_, one for each call to _func_.
> 
> Can you confirm, that it works this way?



Hi Nickie,
Yes it works as you described it. Since function calls cannot be recursive a solution that should cover all cases is to use polyphonic local variables. Then two concurrent function invokations will never use the same instance of the variable. Apart from some simple namespace support there's really no difference between local and global variables. It is only necessary to use polyphonic variables in callbacks that invokes the wait function, but I guess you already knew that - just including it here for completeness.

Cheers,
Nils

PS. I've been a bit busy, but I'll have a look at the troubles you've had with the installation of my programs as soon as I can. DS.


----------



## Nickie Fønshauge (Jun 16, 2006)

Theo, don't get me wrong, I certainly appreciate Nils' work. I have made that clear more than once, but I am only happy to repeat it here :smile: 

Nils, it would certainly be nice to be able to use polyphonic variables. Unfortunately you can not use them in controller callbacks, only in note callbacks. And I can think of other situations (in controller callbacks) than the wait case, where it would be usefull. This is where I miss the local variables. Sure, there are workarounds, but they make function calls rather clumsy. So, a dynamicallly allocated, truely local variable would be a nice feature, if you ever get the time. :smile: 

Cheers

*Nickie*


----------



## kotori (Jun 16, 2006)

Nickie Fønshauge @ Fri Jun 16 said:


> [...]Unfortunately you can not use polyphonic variables in controller callbacks, only in note callbacks. And I can think of other situations (in controller callbacks) than the wait case, where it would be useful. [...]


Hi again Nickie,
You say you can think of other situations than the wait case. I'm not sure I follow you there...
My guess is that wait calls inside controller callbacks are very rare, and if there are no wait calls nothing will be run concurrently so there's no need for per-call instances of local variables. And even if you do use wait inside a controller callback and I would do something about the local variables, you would still have the same problem with global variables, right? Am I missing something? :???: 

Nils


----------



## TheoKrueger (Jun 16, 2006)

Nickie Fønshauge @ Fri Jun 16 said:


> Theo, don't get me wrong, I certainly appreciate Nils' work. I have made that clear more than once, but I am only happy to repeat it here :smile:



heheh Nickie  

I usually avoid reading the techy stuff so my reply was unrelated to the posts above. I'm sure you appreciate Nils as much as we all do. To tell him of something to make his work better only shows that appreciation. :wink: 

Over and out. Chhht.


----------



## Thonex (Jun 16, 2006)

Nickie,

All this function talk is going over my head.... which makes me think I/we could all learn a little from some of your coding. :smile: 

If you're agreeable, I'd love you to post some of you stuff involving functions. I'd like to get into functions more, but when I write a script (they're usually pretty simple) I just end up writing them without even thinking about functions.

How do you guys treat functions? Are they like little mini modules that you use over and over in a script ? I'm just trying to understand where functions fit in it the coding process.

Cheers,

T


----------



## Nickie Fønshauge (Jun 16, 2006)

kotori @ 16/6/2006 said:


> Hi again Nickie,
> You say you can think of other situations than the wait case. I'm not sure I follow you there...
> My guess is that wait calls inside controller callbacks are very rare, and if there are no wait calls nothing will be run concurrently so there's no need for per-call instances of local variables. And even if you do use wait inside a controller callback and I would do something about the local variables, you would still have the same problem with global variables, right? Am I missing something? :???:
> 
> Nils



OK, I didn't know controller callbacks don't generally occur concurrently. So, maybe I can't think of other situations, than the wait case. That doesn't save my day, though, because I do use the wait function in controller callbacks, and that is where I have a problem. I have two input controllers generating data for the same output controller at regular intervals, using the same function. There are of course workarounds: different functions or function parameters used as local variables. It just makes the code somewhat less elegant :wink: Hey, I'm just being demanding here :lol: 

I don't think you can have dynamically allocated global variables. Which instance would you refer to outside the function? Just leave the "global" declared variables the way they are now. That is fine.


----------



## Nickie Fønshauge (Jun 16, 2006)

Thonex @ 16/6/2006 said:


> Nickie,
> 
> All this function talk is going over my head.... which makes me think I/we could all learn a little from some of your coding. :smile:
> 
> ...


Andrew,
I will make my script available, when it is reasionably bug free, which it isn't right now :lol: First I have to deal with these concurrency issues. I am not sure, you can learn a lot, though. It is not all that advanced.

The way Nils has implemented functions, they are basically macros, if that means anything to you. They are reusable code modules, that get inserted into the code at the call point, with the call parameters substituting the definition parameters in the function body. And they don't have to be "mini" :razz: You can also call functions inside other functions. Functions make the code more elegant and easier to comprehend for yourself and for others. And the more elegant and easily comprehensible it is, the less risk there is for bugs.


----------



## kotori (Jun 16, 2006)

Nickie, I see the problem now. However, to be honest I don't think the use case is common enough to make the change worth it in terms of work and the fact that reading the compiled KSP code would become more complex for the cases where one doesn't need per-call variables. Another workaround that you might want to consider is to move code from the controller callback to the release callback. You can do this by generating a short note outside the playable range using play_note which will then trigger the release callback in which you then can use polyphonic variables. This may or may not be practical depending on the nature of your script.

Cheers,
Nils


----------



## Dynamitec (Jun 16, 2006)

Hi Nils!

I've got a a idea that would be easy to add to your editor. Please make a optimized output possible, that means: delete every { } (can be done with ( * * (i know) ), delete every indent and delete every empty line! It would be great for really large scripts. My guitar script now has more than 2000 lines (uncompiled) and 2600 compiled...about 200 or 300 are only spaces...as long as we have this strange behavior of the ksp editor, this would be not bad!

Best Greetings,
Benjamin

PS: If found a litte bug. If you have a "(" between comments, autoindent works a little bit strange. It makes a tab even if the "(" is commented.


----------



## Nickie Fønshauge (Jun 16, 2006)

kotori @ 16/6/2006 said:


> Another workaround that you might want to consider is to move code from the controller callback to the release callback. You can do this by generating a short note outside the playable range using play_note which will then trigger the release callback in which you then can use polyphonic variables. This may or may not be practical depending on the nature of your script.


Nils,

that is an interesting idea, that might prove to be usefull. Thanks!


----------



## Thonex (Jun 16, 2006)

Nickie Fønshauge @ Fri Jun 16 said:


> kotori @ 16/6/2006 said:
> 
> 
> > Another workaround that you might want to consider is to move code from the controller callback to the release callback. You can do this by generating a short note outside the playable range using play_note which will then trigger the release callback in which you then can use polyphonic variables. This may or may not be practical depending on the nature of your script.
> ...



Awesome idea!!!

I love this forum!!  


CHeers,

T


----------

