# KScript Editor 1.4.3



## gregjazz (Apr 23, 2010)

Yeah, not being able to pass/return parameters is really bad--it's so essential.


----------



## Dynamitec (Apr 24, 2010)

*Re: KSP - Kontakt 4.1 beta*

Hi Nils,

why not introducing:

sub xyz

end sub

and:

call xyz

It would be the easiest way for now.

However I would prefer the full functionality of your editor, however it will automatically rewrite everything so it's not inlined anymore  This would keep scripts small even if compiled.

Cheers,
Benjamin


----------



## Big Bob (Apr 24, 2010)

*Re: KSP - Kontakt 4.1 beta*

Hi Benjamin, when a K4.1 'function' is defined in one script slot can it be 'called' from another script slot? Or, is it only available to be called from the script slot that defined it?

I was just thinking about completing work on an "Interscript Operating System', IOS (that I started writing about a year ago before my 'temporary' interruption with my move). My IOS would have provided for a 'common' subroutine library that could be defined in one script slot but could be referenced from any of the 5 slots without duplicating the code. I thought such an approach could be quite useful for things like the Math Library and such.

However, if NI intends to add real user functions or at least subroutines, I better hold off a while. And, as Nils has indicated in his post, this latest addition to K4.1 will also require some way to resolve the potential ambiguity when scripts are written with user functions. It's a pity NI didn't use the keyword 'subroutine' or 'procedure' instead of 'function'. Unless of course they ultimately intend to upgrade this feature to actually return a value (even if they don't allow passing other parameters). 

And Nils, since you never got around to having your editor return values either, wouldn't another solution be to rename the KScript 'function' to something else like 'procedure'. Of course all older scripts would then have to be edited with a 'search and replace' to update them to the new K4.1 KScript format.

Benjamin, do you have any info as to what NI's future plans for this stuff might be? This is all very exciting but I do wish NI would share with us what they are intending to do (so that we could better 'anticipate' what to do with the tools we have been forced to create to work around the current KSP limitations). But, I suppose that's too much to hope for :lol: 

God Bless,

Bob


----------



## gregjazz (Apr 24, 2010)

Maybe there's a way of automating PGS variables into the new callback functions, to simulate passing/returning parameters?


----------



## kotori (May 5, 2010)

*Re: KSP - Kontakt 4.1 beta*

Now I have updated KScript Editor to support all Kontakt 4.1 KSP features. The function definition syntax for functions that are to be inlined (the ones one has been able to use up to now) and functions that are invoked using the new "call" keyword in Kontakt 4.1 are identical (I wanted every script using standard KSP to work as it is). In fact, you can have one function and invoke it both ways in one and the same script:

*function* play_octave
``play_note(EVENT_NOTE + 12, EVENT_VELOCITY, 0, -1)
*end function*

*on note*
``*call* play_octave```_{ this is a true function call - a new feature in Kontakt 4.1 }_
``play_octave````````_{ this leads to the body of the function being inlined here }_
*end on*

I have actually not had the chance to test but I assume that the new user-defined functions in Kontakt 4.1, being limited in some other regards (eg. no parameters), do not support local variable declarations. But if you use KScript Editor you can, just like before, use local variable declarations thanks to its extended syntax. My script compiler also automatically does a topological ordering of the function call graph. This lets it reorder the function definitions in the compiled code in such a way that Kontakt never complains about the order of definitions. 8)

Since I got some information from NI describing the new KSP features I just have to wait until I can get it confirmed that those things are no longer secret before I release my update. Does anyone of you who have installed the beta know if it included any K4.1 KSP documentation?


----------



## Mark Belbin (May 5, 2010)

*Re: KSP - Kontakt 4.1 beta*

Nils,

Thanks so much for your excellent contributions. I've hitherto been trudging along and scripting as I always have, but I think with these developments from NI and yourself, I'll soon be ready to make some great progress as a script writer.

Many thanks!

Mark


----------



## gregjazz (May 5, 2010)

Awesome, that's perfect!


----------



## kotori (May 6, 2010)

*Re: KSP - Kontakt 4.1 beta*



Big Bob @ Fri May 07 said:


> Hey Nils, what a lovely solution o-[][]-o . When K4.1 functions are defined, I presume they generate no runtime code until they are called? I guess the definitions are just held somewhere in some compressed form?



Hi Bob

Yes, that's right. Here's an example: you define functions A, B, C, D, E in that order. A invokes B och D, B invokes C, C and D don't invoke any other function, and E is never invoked (using "call" at least). The compiler will then include in the compiled code the definitions of D, C, B, A in that order (to make sure that a function is always defined before it's used). E will not be included. Function definitions are normally placed just after the 'on init' callback in the compiled code.

I am contemplating adding parameter support to the new native functions, but I have not done this yet. For example, maybe one could translate this:

*function* myfunc(a, b)
``message(a * b)
*end function*

*on note*
``*call* myfunc(5, 6)
*end on*

into this:

*on init*
``*declare* myfunc_arg_a _{ automatically generated declaration for function parameter }_
``*declare* myfunc_arg_b _{ automatically generated declaration for function parameter }_
*end on*

*function* myfunc``
``message(myfunc_arg_a * myfunc_arg_b)
*end function*

*on note*``
``myfunc_arg_a := 5```_{ automatically generated assignment }_
``myfunc_arg_b := 6```_{ automatically generated assignment }_
``*call* myfunc
*end on*

Maybe one could make it so that the automatically generated variables could be read from the outside using for example "myfunc.arg_a" or just "myfunc.a" (even though the risk for name clashes is greater in the latter example). Then one could even use them to pass result values out of the function. One problem would be that it's easy to forget that there is no real call stack so if one uses the wait call in the middle of a call:ed function and there is a reentry the previous parameters would be overwritten. One has the same problem for local variable declarations in functions invoked using the call keyword though. I could perhaps partially get around this problem by allowing function parameters to be declared polyphonic.

If you or anyone else here have any thoughts about this I'd love to hear.

Cheers,
Nils


----------



## Dynamitec (May 7, 2010)

*Re: KSP - Kontakt 4.1 beta*

Hi Nils,

correct me if I'm wrong, but didn't we have the same problem (no real call stack) with the old inlined functions as well? 

I too think polyphonic variables as parameters would be important in this case as well.

Cheers,
Benjamin


----------



## kotori (May 7, 2010)

*Re: KSP - Kontakt 4.1 beta*



Dynamitec @ Fri May 07 said:


> didn't we have the same problem (no real call stack) with the old inlined functions as well?


Yes, but in that case it was hopefully a bit more obvious that it's an inherent limitation when using function inlining. And with function inlining it was also possible to pass literals or different variables as arguments. For example "func(5)" would never lead to problems caused by the parameter, but "call func(5)" could lead to problems if one doesn't think through the wait instructions.


----------



## Big Bob (May 8, 2010)

*Re: KSP - Kontakt 4.1 beta*

Hi Nils,



> I could perhaps partially get around this problem by allowing function parameters to be declared polyphonic.



Yeah, the parameter thing is a real problem. When I was working on my IOS subroutine machinery I originally used polyvars but then I ran into some situations where K3 would actually return the wrong value (no doubt this was some kind of K3 bug which may have been corrected by now). But, as a result of this problem, I changed to using event parameters. This was unfortunate because it limited the number of parameters to 4 per call. And, besides that, you may recall that event parameters aren't exclusively available to user scripts either (NI also uses them in certain annoying situations).

Polyvars (and/or event pars) theoretically will work when using the RCB for pseudo calling because each 'call' triggers a RCB and therefore each 'link' to the subroutine body has its own associated note event. However, I don't know how K4.1's calling scheme works (haven't downloaded 4.1 yet). Can one call a 'function' from say a ui callback? If so, there may not be any note event associated with the call and polyvars or event pars may not have a frame of reference tied to the call.

Before I took my long moving sabatical, I was toying with the idea of trying to implement a parameter stack someway but, I never got around to it. Have you tried to cook up something like that? 

As soon as I get the time to download K4.1 and try to get my brain back in gear, maybe I can try to revisit some of my former ideas on handling parameters and if I come up with anything, I'll post my thoughts. But, knowing you, you will probably find some elegant solution to the problem long before that :D 

To be continued .....

Bob


----------



## kotori (May 10, 2010)

*Re: KScript Editor 1.4*

I noticed that the KSP Documentation was included in the latest Kontakt 4.1 public beta, so now there should be no problem releasing the new KScript Editor: 

:arrow: *Download KScript Editor 1.4* (for Windows and OSX)

Excerpt from the version history:
Support for polyphonic aftertouch callback in Kontakt 4.1
Support for native user-defined functions in Kontakt 4.1 and the "call" keyword (including automatic reordering of function definitions using topological sorting of the call graph). 
Support and documentation for new variables, functions and callbacks in K4.1
Fixed: a problem in the html-export
Fixed: call tips (invoked using Ctrl+Shift+Space) was confused by multiple functions on the same line.
Notes about user-defined functions:
 if a function is invoked using "myfunc" it is inlined
 if a function is invoked using "call myfunc" it is not inlined and the native features in Kontakt 4.1 are used.
 functions that are somewhere invoked using "call" may not have any parameters
 functions that are invoked using "call" may have local variable declarations
It should be noted that the local variables in call-ed functions are implemented as global variables under the hood, so they are not unique to each function invokation. Therefore one must take care when dealing with re-entrant functions.


----------



## Dynamitec (May 10, 2010)

Thanks Nils! You are the hero of all scripters! I know, that I repeat myself when it comes to this topic, but without your editor really complex scripting simply wouldn't be possible!

Thanks a lot!
Benjamin


----------



## Bo Clausen (May 11, 2010)

*Re: KScript Editor 1.4*

Dear Nils

Thanks a lot - =o 

Time for a new PayPal donation :wink: 

Best regards
Bo


----------



## kotori (May 29, 2010)

*Re: KScript Editor 1.4*

I have now uploaded a new version for Windows and OSX: :arrow: *KScript Editor 1.4.2*

Now my extended syntax should work well in functions that are invoked using the "call" keyword and I also fixed a bug that caused local variables to be left out from the compiled code if one used inlined functions inside functions invoked using "call".


----------



## EvilDragon (May 30, 2010)

Kotori, you have a typo in the KSP Reference list. I know it's easy to manually correct by editing the ksp_doc.txt, but you might wanna know.

Variables->Kontakt 4->$LFO_TYPE_RANDO


----------



## kotori (May 30, 2010)

*Re: KScript Editor 1.4.2*

Thanks ED. It'll be fixed in the next version (if you want to use it before then, please correct it in ksp_util.txt too). The reason for the mistake was actually a typo in NI's KSP manual.

Cheers,
Nils


----------



## EvilDragon (May 30, 2010)

BTW, one more question/suggestion:

when I move through indent tabs, why does cursor behave like it's moving through individual spaces? I dunno, the majority of text editors I have used in the past treat moving through tabs like you're jumping over n individual spaces (n being indentation width, in this default KS case, 4).

It would be cool if your editor behaved like that as well! That is, when going left-right with arrow keys, to move by n spaces, and also when deleting tabs with Backspace.


Also, there is one case that your compiler did wrong, but which works when done in Kontakt's script editor (probably an exception needs to be introduced):

Try this code:

on init
declare !char[2]
!char[0] := "{"
!char[1] := "}"
end on

When you compile this and paste into Kontakt, !char[1] won't be declared!!! This is bad. So, when comment symbols are found between the " " marks, they shouldn't be considered as comments, and should be colored like ordinary text string!


----------



## kotori (May 30, 2010)

*Re: KScript Editor 1.4.2*

Most text editors I have used treat tabs as spaces by default, but it could be because I'm a python programmer (you cannot mix tabs and spaces in Python). I might add an option to treat tabs like tabs in the future, but it's not a high priority since it takes some extra work to get everything right in the compiler then. If you want to indent/dedent an entire block of code you can select it and press tab or shift+tab.

The first compilation phase uses only regular expressions so it's a bit difficult to find a definitive solution to the string problem that you describe. For now I solved it by adding a negative lookbehind assertion to the regexp that says that { immediately preceeded by ' or " does not constitute a comment. I uploaded an update (only for Windows so far - I hope that's what you are using).


----------



## EvilDragon (May 30, 2010)

Yup, WinXP here 

Thanks! I think that your exception is gonna work well, perhaps adding an exception which declares that } continued by ' or " does not constitute comment ending, would also help.


----------



## EvilDragon (May 30, 2010)

Something's broken.

I edited a script which included some functions from Big Bob's math library (using of course import command)

Now, when I remove the import "....." command, it doesn't let me compile the script at all! It simply finds the first empty line (or a comment) and says "Unknown function". Yes, it forces me to have no spaces between logical command blocks!

I bet this is connected to the RegExp hack we did to bypass that bug I reported earlier!


----------



## kotori (May 30, 2010)

EvilDragon @ Sun May 30 said:


> Something's broken.



Now it's fixed (I replaced the V1.4.3 file so clear your cache and download it again). I accidentally introduced an error that affected scripts that have no function definitions.


----------



## EvilDragon (May 30, 2010)

You rule, man! 


One tiny suggestion - could you add an option to the installer "Don't create Start Menu items" when you can? Thanks!


----------



## nlundberg (Jun 1, 2010)

*Re: KScript Editor 1.4.2*

Hi Nils,

thanks for a really cool app! It very pleasant to learn scripting with it. 

However, in 1.4.2, on OSX/Mac, I suddenly can not compile older scripts. At comments, it stops and says "Compilation error Unknown function: {MyComment}". So as it is now I can not compile my scripts, and since they are not ready to be read by Kontakt I can not use them! This also happens when I try to compile the SIPS_KS code.

Here is an example:



> on init
> set_script_title("Automod v11")
> set_ui_height(8)
> message("")
> ...



I have stepped back to 1.4; it works fine there.

Thanks for a great app!

Best,

Nicklas.


----------



## EvilDragon (Jun 1, 2010)

1.4.3 is up, download it :D


----------



## nlundberg (Jun 1, 2010)

Thanks, but the mac download button gives me 1.4.2. I guess it is not translated yet.


----------



## kotori (Jun 1, 2010)

nlundberg @ Tue Jun 01 said:


> Thanks, but the mac download button gives me 1.4.2. I guess it is not translated yet.



If you reload now, you should see the right link.


----------



## nlundberg (Jun 1, 2010)

Great!

A minor point (in the OSX version): 

When you choose file/open my saved script are greyed out, and you have choose enable all files on order to select and load.

This is because the file extensions are hidden by default in OSX, but the files are still normal .txt files, saved from KScript editor.

I tried to open a text file with no .txt ending from another app (TextWrangler) and in the open window it shows up as a valid file.

As I said, this is obviously not that important, but I just thought you should know.

Nicklas.


----------



## Dynamitec (Jun 2, 2010)

I just wanted to let you know that the silent abort if a empty
if or case construct is used is fixed in 4.1!


----------



## kotori (Jun 2, 2010)

Great! Thanks for the heads-up Benjamin.  
Might be good to keep the optional detection system in place for a while though until everyone has upgraded to K4.1.


----------



## EvilDragon (Jun 9, 2010)

Nils, I think I found another bug or two.

When I use my constants library and import it into a script, I get two on init callbacks compiled (!), and of course Kontakt can't apply the script, I have to remove that each time manually! :/

Second bug refers to syntax coloring. If I begin a comment DIRECTLY after ANY character, it will not comment out the text. Example:

{declare mapping[MAPPED_KEYS] := ( 60 , 61 , 62 , 63)}

works propertly, but:

declare mapping[MAPPED_KEYS] := ( 60 , 61 , 62{, 63})

doesn't color propertly!


----------



## Dynamitec (Jun 9, 2010)

Hi EvilDragon,

If you use an import you should write all declarations inside a function rather than an on init callback. You simply call that function in the on init callback later.

For example:

constants.txt:

function OnInit
declare global const test1:= 1
declare global const test2:= 2
declare global const test3:= 3
end on

program.txt:

import "constants.txt" as CONSTANTS

on init
CONSTANTS.OnInit
end on


----------



## EvilDragon (Jun 9, 2010)

Cool, thanks!


EDIT: wait... it doesn't compile the script again :/


----------



## Dynamitec (Jun 9, 2010)

what's the problem?


----------



## EvilDragon (Jun 9, 2010)

Using my constants lib, and then in the script:


```
import "Constants.txt" as constants

on init
    constants.Init
	declare count
	declare !char[4]
		!char[0] := "."
		!char[1] := "["
		!char[2] := "]"
		!char[3] := "|"
	declare @t

	declare ui_label text (3,1)
	declare ui_value_edit Low (0,127,NOTE)
	declare ui_value_edit High (0,127,NOTE)
```

Says NOTE hasn't been declared. I modified the constants.txt and replaced on init with function OnInit, and replaced all "const" with "global const" (what's the difference anyways, "global" doesn't color as a keyword...?)


----------



## Dynamitec (Jun 9, 2010)

If you use 'declare global' inside a function, the variable or constant is a 'global' one. This means you can access it from other functions as well. If you don't use global they are (semi) local variables of that function.

Ok try this:
1) remove 'global' again.
2) use:

Constants.txt:

macro Constants.OnInit
{ your declarations}
end macro

3) use this code:

import "Constants.txt" { no as }

on init
Constants.OnInit
end on

Does this work?


----------



## EvilDragon (Jun 9, 2010)

Yup, this works!

Thanks for all the help, Ben!


----------



## Dynamitec (Jun 9, 2010)

No problem  But you really have to thank Nils! Without his editor such things wouldn't even be possible! o-[][]-o


----------



## EvilDragon (Jun 9, 2010)

That goes without saying! Kontakt could really use a LOT of optimizations when handling big scripts from its own editor. Perhaps they should just implement Nils' editor into it and make him their employee


----------



## Dynamitec (Jun 9, 2010)

In K4 things have changed quite a bit, since you can now simply link a text file to a script slot. That way you don't have to copy and paste a compiled script each time you change it.

With this line in Nils Editor for example:

{#pragma save_compiled_source C:\Users\Benjamin\Documents\Native Instruments\Kontakt 4\scripts\Electri6ity_Slot1_Performance.txt}

You can force the editor to ouput a text file when you compile the script. You only have to press restart engine in Kontakt ([!] button) and you are ready. This is a incredibly speed up!


----------



## EvilDragon (Jun 9, 2010)

I presume that's the only location where I can have the scripts, right? Because from what I gather K4 only looks in that folder itself.


----------



## Dynamitec (Jun 9, 2010)

Right...Kontakt allows to link slots to txt files inside that folder.


----------



## Dynamitec (Jun 10, 2010)

Hi EvilDragon,

you have code optimization turned on. And since you'll never use any of the buttons in the script they'll get removed.

Cheers,
Benjamin


----------



## EvilDragon (Jun 10, 2010)

Man, I do make some stupid mistakes, eh? Thanks AGAIN, Ben!


----------



## Dynamitec (Jun 10, 2010)

We all make them! Lol, belive me man, I did a lot more stupid Kontakt or KSP related mistakes similar to that one in the past


----------

