# KSP Q&A



## Truthtrader (Jun 9, 2022)

Hey everyone, 

Hope you are all well.

I thought I would post a thread that I and other users can use as a Q&A for KSP instead of posting a new thread every single time. 

90% of my questions have simple answers so it is a waste for a new thread.

Mods let me know if this is ok or not. 

Thanks everyone


----------



## Truthtrader (Jun 9, 2022)

Hey. 

First and second question on the thread.

What would be a good resource to learn more about "code" aspects of KSP. meaning that variables, arrays, for loops are pretty universal concepts. 

I ask because I don't want to invest time in a language that is too far removed from ksp.

So which language would you recommend as a good basis to develop knowledge from?

Second question.

How do macros and snippets differ and what is the benefit and draw back of each?

Thanks guys.


----------



## EvilDragon (Jun 9, 2022)

KSP syntax is (very) loosely based on Pascal, so if you're looking for closest match, that would be it.

Macros are compile time constructs that are basically text replacement tools. Snippets are simply making writing the code faster, for example you type "ife" and press Tab and you get the whole if-else construct dropped in right up.


----------



## Truthtrader (Jun 9, 2022)

EvilDragon said:


> KSP syntax is (very) loosely based on Pascal, so if you're looking for closest match, that would be it.
> 
> Macros are compile time constructs that are basically text replacement tools. Snippets are simply making writing the code faster, for example you type "ife" and press Tab and you get the whole if-else construct dropped in right up.


Thanks for the advice regarding pascal, will find a text book and study it.

so to my understanding snippets are basically macros that you dont have to have somewhere in the code. 

What i mean is that i have a file that i have stored macros in, that gets imported into my on init.

So essentially if i write these macros as snippets then i wouldnt need that macro template file?


----------



## EvilDragon (Jun 9, 2022)

I would not advise to do that. Snippets are not like KSP macros at all. Snippets just make writing code faster. Macros can do some other things, like repurposing a single block of code to do different things based on arguments you put into it. They are like apples and oranges, please don't confuse them. Keep your separate file for importing. Snippets are not changing anything there.

That said, there are only syntax similarities with Pascal. Not sure if getting a Pascal book would help you much with KSP, the purpose of each language is quite different.


----------



## Truthtrader (Jun 9, 2022)

EvilDragon said:


> I would not advise to do that. Snippets are not like KSP macros at all. Snippets just make writing code faster. Macros can do some other things, like repurposing a single block of code to do different things based on arguments you put into it. They are like apples and oranges, please don't confuse them. Keep your separate file for importing. Snippets are not changing anything there.


Ok thank you for clearing that up


----------



## d.healey (Jun 9, 2022)

Truthtrader said:


> What would be a good resource to learn more about "code" aspects of KSP. meaning that variables, arrays, for loops are pretty universal concepts.


I have some videos on YouTube and I sell some KSP tutorials at my website, link in my signature.


----------



## Truthtrader (Jun 10, 2022)

Its me Cale. 

I have just gone through your kontakt masterclass and going to go through it again as I am struggling to set up my own template like yours with the macros functions etc

I'm getting alot of syntax errors and I still need to get my head around using arrays to build controls quicker. 

Your course was invaluable to me, really it has helped me so much. 

I think alot of my problem is understanding the basis of coding. Ie) 

Loops arrays functions etc. 


d.healey said:


> I have some videos on YouTube and I sell some KSP tutorials at my website, link in my signature.


----------



## RogiervG (Jun 10, 2022)

learn to crawl before walking. (and don't even try to run)


----------



## Truthtrader (Jun 10, 2022)

RogiervG said:


> learn to crawl before walking. (and don't even try to run)


Thanks man. I appreciate that. I guess it's natural to try do the most complicated thing possible.

You right I have been killing myself, I think I need to put out some simple instruments and get some confidence under the belt


----------



## bcslaam (Jun 11, 2022)

Qu: Is there a script available that can change snapshots, triggered by midi or automation?

A way to remotely change the settings of a complex script based instrument, in my case 8dio Bazantar, as a snapshot/preset without instrument banks?

I'm talking about the settings we cant assign midi to within the instrument gui and are mostly not available in automation either. If necessary, even reloading the samples is acceptable, if its done efficiently. ie more efficiently than using a separate coupled instance in VEPro and totally reloading everything.

I have Braintree Mind Control but that doesnt do this without the use of instrument banks and thats a real RAM hog when you have to load an instance per preset.

Is there any way to change snapshots via automation? That would be the ideal way for a Nuendo/VEPro big orchestral template user. Then we're not limited by midi also. Care to speculate whats coming in v7 in this regard?

Perhaps another way is to inject code into existing instruments ksp. Alas most of the good ones are locked. But for those that allow it can it be somehow added? I guess thats what the global ksp function is for. Comes back to my first qu tho really.


----------



## EvilDragon (Jun 12, 2022)

There isn't any such script. You cannot change snapshots remotely via MIDI nor automation, except as you mentioned already, the only way is via instrument banks after you resave snapshots as NKIs.


----------



## Truthtrader (Jun 12, 2022)

Hello to the overlords of KSP. Hope you are all well.

Currently i am following David's framework for writing code, i dont want to say what it is as its part of his course.

I wanted to ask how other people are organising their code.

Currently i have a "template" file that has macros that i can call using pragma.
Other then using code folding with headings in sublime are their any other ways that you 
guys are organising code?

I understand that you guys prefer to keep your workflow undercover because you have put them time and effort to deveop it, but any tips and tricks that youu are happy to share i will definitely appreciate.

Thanks


----------



## Truthtrader (Jun 13, 2022)

Struggling with this......

i want ONE ui control to control TWO or more parameters?

below i want one control to change (Group 2 Vol and Group 1 effect slot 1 EQ GAIN)

`declare ui_slider $sli_group_2 (0, 650000)
set_control_par_str(get_ui_id($sli_group_2), $CONTROL_PAR_PICTURE, "vol")
set_control_par(get_ui_id($sli_group_2), $CONTROL_PAR_MOUSE_BEHAVIOUR, -500)
make_persistent($sli_group_2)
read_persistent_var($sli_group_2)
move_control_px($sli_group_2, 50+$i, 50)
on ui_control($sli_group_2)
set_engine_par($ENGINE_PAR_VOLUME, $sli_group_2, 1, 0, -1)
{Stuck here}
set_engine_par($ENGINE_PAR_GAIN1, value, 0, 0, -1)
end on`


----------



## EvilDragon (Jun 13, 2022)

That's correct. you need to scale your $sli_group_2 to whatever gain you want. So, some math required (linear interpolation).


----------



## Kent (Jun 13, 2022)

EvilDragon said:


> There isn't any such script. You cannot change snapshots remotely via MIDI nor automation, except as you mentioned already, the only way is via instrument banks after you resave snapshots as NKIs.


this makes me so sad


----------



## bcslaam (Jun 13, 2022)

Kent said:


> this makes me so sad


Me too.
Although it confuses me how the inaccessible settings of an instrument, including its snapshot, can be stored in a daw directly or even via a coupled vepro instance when we save a project. But that its not accessible to automation even, let alone within ksp.

The fact that a daw can cause vepro to access those settings/snapshot and store it remotely would suggest it is indexed in the vsti api.

I wish we knew if NI intends to develop Kontakt more in this regard. Seems like now they only scratch the surface of what used to be a more capable coding team.


----------



## EvilDragon (Jun 14, 2022)

You really don't want patch changes available via host automation. Those events are too fast, and when disk access is involved, it creates a loooooot of issues.



bcslaam said:


> The fact that a daw can cause vepro to access those settings/snapshot and store it remotely would suggest it is indexed in the vsti api.


That's the wrong way to look at it. This is simply storing plugin data in a chunk - not the same thing as physically loading snapshot from a hard drive. All plugins do this otherwise your DAW projects would all load with an init patch.



bcslaam said:


> Seems like now they only scratch the surface of what used to be a more capable coding team.


You couldn't be more wrong about this one. But indeed user-facing priorities lie elsewhere.


----------



## Truthtrader (Jun 14, 2022)

I dont know if im being stupid.

i have "imported big bobs maths into my script, but i am just getting alot of undeclared errors? what do i need to do?


----------



## Truthtrader (Jun 14, 2022)

Truthtrader said:


> I dont know if im being stupid.
> 
> i have "imported big bobs maths into my script, but i am just getting alot of undeclared errors? what do i need to do?


I fixed it thanks


----------



## EvilDragon (Jun 14, 2022)

Maybe also mention how you fixed it, for posterity.


----------



## Truthtrader (Jun 14, 2022)

Truthtrader said:


> I fixed it thanks


I just included 

SetMathMode(ALL,DD) in on init


----------



## Truthtrader (Jun 14, 2022)

EvilDragon said:


> That's correct. you need to scale your $sli_group_2 to whatever gain you want. So, some math required (linear interpolation).


so i found this on an older forum post, i dont just want to copy paste as it serves no purpose, i would like to understand the maths/what it is doing.

the code is 


```
set_engine_par($ENGINE_PAR_GAIN1, (300000 +$sli_group_2*35/50), 0, 0, 1)
```

this equation does exactly what i want, but the inverse.

i want GAIN1 to go from 500000 - 300000.

Regardless of that i would like to understand what (300000 +$sli_group_2*35/50) is doing


----------



## Truthtrader (Jun 14, 2022)

Truthtrader said:


> so i found this on an older forum post, i dont just want to copy paste as it serves no purpose, i would like to understand the maths/what it is doing.
> 
> the code is
> 
> ...


from what i understand

(300000 +$sli_group_2*35/50)

(start_point+$ui_slider*scale_amount/+or-direction)

I got it to work the way i want the answer is:

(500000 +$sli_group_2*10/-50)

can you help me understand what

scale_amount and +or- direction actually is please


----------



## polypx (Jun 14, 2022)

500000 is no gain for EQ gain, ie. gain at 0.
So if the slider*10/50 is positive you will add to that, boost the frequency.
If slider*10/50 is negative you will subtract from that, cut the frequency.

The 10/50 is scaling the slider range, by 20% in this case.


----------



## Truthtrader (Jun 14, 2022)

polypx said:


> 500000 is no gain for EQ gain, ie. gain at 0.
> So if the slider*10/50 is positive you will add to that, boost the frequency.
> If slider*10/50 is negative you will subtract from that, cut the frequency.
> 
> The 10/50 is scaling the slider range, by 20% in this case.


oh ok so the 10/50 is merely the percentage

ie) 20/50 will be 0.4 = 40%

Thanks polypx

Thanks Dragon


----------



## Maximilian Moll (Jun 14, 2022)

New question:
Is it possible in any way to subtract from string variables? Like you can add with "&".
To be precise, I have an instrument folder with nki files and a folder with midi files. Both in the same directory, so with $GET_FOLDER_PATCH_DIR I can't access the midi folder, since it's technically outside the patch directory.
Any way to access it? Or do I need to move one of the folders so the order works?


----------



## EvilDragon (Jun 14, 2022)

You can use "../" to move a folder level up from the path that you get with $GET_FOLDER_PATCH_DIR.


----------



## Maximilian Moll (Jun 14, 2022)

EvilDragon said:


> You can use "../" to move a folder level up from the path that you get with $GET_FOLDER_PATCH_DIR.


Perfect! Exactly what I was looking for. Thank you!


----------



## Truthtrader (Jun 20, 2022)

Hope everyone had a good weekend. 

I have been struggling with this for the last few hours and have done the prerequisite searches but can't find anything.

I am trying to skin a menu, but every time I hover over the menu the image drops down slightly but noticeably. 

The menu text stays in place. 

Does anyone understand why this happens

Thank you


----------



## EvilDragon (Jun 20, 2022)

You probably have misaligned frames in the PNG. The graphic is the same as for the button/switch (6 frames), but only frames 1, 3 and 5 are actually used for ui_menu.


----------



## Truthtrader (Jun 20, 2022)

EvilDragon said:


> You probably have misaligned frames in the PNG. The graphic is the same as for the button/switch (6 frames), but only frames 1, 3 and 5 are actually used for ui_menu.


I recreated the menu png in knobman, and it seems fine, but if i increase the height of the menu png past 17px the the menu text does not fit into the png correctly


----------



## EvilDragon (Jun 20, 2022)

You need to adjust vertical offset of text with $CONTROL_PAR_TEXTPOS_Y.


----------



## Truthtrader (Jun 20, 2022)

EvilDragon said:


> You need to adjust vertical offset of text with $CONTROL_PAR_TEXTPOS_Y.


Thank you so much


----------



## Scripter (Jun 23, 2022)

A question from me according the on ui_control of menus:


```
on init
make_perfview
declare ui_menu $Menu
add_menu_item ($Menu, "Test Entry 1",0)
add_menu_item ($Menu, "Test Entry 2",1)
end on


on ui_control($Menu)
if($Menu = 0)
message("Hi")
else
if($Menu = 1)
message("Bye")
end if
end if
end on
```

There shurely is a way to make the on ui_control shorter?


----------



## Mike Greene (Jun 23, 2022)

Scripter said:


> There shurely is a way to make the on ui_control shorter?


If $Menu is a switch, then the only options are 0 or 1, so you don't need the "if" conditional after the else line. So you could do this:


```
on ui_control($Menu)
   if($Menu = 0)
      message("Hi")
   else
      message("Bye")
   end if
end on
```

If $Menu can have values beyond 0 and 1, then "select" is a good option:


```
on ui_control($Menu)
   select ($Menu)
      case 0
         message("Hi")
      case 1
         message("Bye")
   end select
end on
```


----------



## EvilDragon (Jun 23, 2022)

Or you can just use a string array,. if menu has 0-based consecutive entries.


----------



## Mike Greene (Jun 23, 2022)

EvilDragon said:


> Or you can just use a string array, if menu has 0-based consecutive entries.


Yes, although given that this is a basics thread, I'd recommend the simpler to understand method first.


----------



## Scripter (Jun 24, 2022)

EvilDragon said:


> Or you can just use a string array,. if menu has 0-based consecutive entries.


Would be interested in that method too if you could further explain, maybe with an little example, that usually helps the most for understanding how something works. Im always eager to learn :D


----------



## EvilDragon (Jun 24, 2022)

```
on init
    declare !txt[2] := ("Foo", "Bar")

    declare ui_menu myMenu
    add_menu_item(myMenu, "First", 0)
    add_menu_item(myMenu, "Second", 1)
end on

on ui_control (myMenu)
    message(txt[myMenu])
end on
```


----------



## rawl747 (Nov 21, 2022)

Problem: 
I want to extract the first three characters of the string value that the get_folder(<path-variable>) function returns when specifying $GET_FOLDER_LIBRARY_DIR as the <path-variable>. 

In other languages beside KSP, this would typically be a done using a "substr" function where I passed the entire string and a value specifying how many characters to "extract" and typically telling the function to start from the beginning (or from another starting point).

I can't seem to find any sort of text (or string) manipulation functions in the KSP reference similar to "substr".

Suggestions?


----------



## EvilDragon (Nov 21, 2022)

KSP doesn't do that sort of thing at all.


----------



## rawl747 (Nov 22, 2022)

EvilDragon said:


> KSP doesn't do that sort of thing at all.


Thanks ED for the quick answer. 

I am trying to find a way to conditionally select from three different sets of custom controls and backgrounds based on which of three sets of Instruments (from a single library) a given Instrument gets loaded. 

Each "Instrument Set" has its own folder with the first three letters denoting one of the three. I guess I could just test for the full folder name after grabbing it using the get_folder($GET_FOLDER_LIBRARY_DIR) 
function in the "on init" section as an Instrument loads.

Shocked that there are zero string manipulation functions available in KSP. I guess that I am spoiled by my experience with "real" programming languages, LOL.

Thanks again ED,
Rawl


----------



## EvilDragon (Nov 23, 2022)

KSP is ran and interpreted (it's not compiled) in realtime audio thread, so string operations come with a performance cost.

Anyways, you can sort your problem out by having a hidden parameter (you hide it for release but keep it visible during dev) that tells you the type of the patch, then you can do whatever you want based on the value of that parameter. This is a much leaner solution, I'd say.


----------



## rawl747 (Nov 23, 2022)

EvilDragon said:


> KSP is ran and interpreted (it's not compiled) in realtime audio thread, so string operations come with a performance cost.
> 
> Anyways, you can sort your problem out by having a hidden parameter (you hide it for release but keep it visible during dev) that tells you the type of the patch, then you can do whatever you want based on the value of that parameter. This is a much leaner solution, I'd say.


Understood re: performance issues, good point. 

When you say "hidden parameter" do you mean a hidden control of some sort?


----------



## EvilDragon (Nov 23, 2022)

Yes. Whose value would then be also saved in the NKA file you're loading.


----------



## rawl747 (Jan 2, 2023)

EvilDragon said:


> Yes. Whose value would then be also saved in the NKA file you're loading.


So, here is how I solved this particular scripting problem which was how to support the loading of custom skins (wallpapers) and control pictures for one of three different "themes" based on the sample source each set of instruments is using. 

When an instrument loads, depending on which of the three themes desired, the correct theme of graphics pictures get set for the custom GUI.

I name the first sample group in each instrument with a 3-letter abbreviation corresponding to the sample source (one of three vintage hardware samplers). 

So, I have three sets of graphics including a wallpaper skin specific to each sample source and control pictures that have a color scheme that matches the skin for that sample source theme. Every single instrument in a theme has the first group appropriately named.

I also created a "development" theme (all gray) for when a new instrument is created before that first group can be named. It keys off of the default name for Group 0 which is "Layer 1".

Early (very) in the on init section, I declare a text variable to hold the name of Group 0, assign it, and then concatenate that variable at the end of the picture name referenced in the "set_control_par_str", "$CONTROL_PAR_PICTURE" statement for each graphic element after it is declared. 

Here are some code extracts that demonstrate the basic technique:


```
on init

...
    {Dynamic loading of control graphics based on Group 0 name - Rawl Gelinas 2023}
    declare @InstrumentType
    @InstrumentType := group_name(0)
    declare @count
...
    {set wallpaper according to @InstrumentType}
    set_control_par_str($INST_WALLPAPER_ID, $CONTROL_PAR_PICTURE, "my_library_skin_" & @InstrumentType)
...
    {Set the custom picture for a control}
    {actually one example of many sliders, sliders with knob pictures, switches and menus, etc}
    declare ui_slider $amp_env_velocity (0,1000000)
    $count := get_ui_id($amp_env_velocity)
    set_control_par_str($count, $CONTROL_PAR_PICTURE, "knob_radial_" & @InstrumentType)
...
end on
```

Of course, there must exist corresponding picture files in the Resources/pictures folder with the correct names for each "theme" so that the @InstrumentType variable added to the text part of each picture file name yields a valid filename. In my case there are 4 versions of each picture file plus it's associated text descriptor file.

If there is a safer or more elegant way of solving this problem, it has escaped me. And, I have tried many, LOL. 

Thanks again to ED for his insight and advice.

I hope this may help the next person...
Rawl


----------



## Lionel Schmitt (Jan 3, 2023)

Is there a way to select multiple clusters of groups at once that are NOT located next to each other?
Like a cluster of 10 groups on top and then another 10 groups further to the bottom.

In the Windows explorer it's possible by holding ctrl and dragging your mouse over the desired items, but I have no idea how it works in Kontakt.

Showing what I mean here:

I can only select clusters of groups that are next to each other, without gaps.

Then I end up just selecting the remaining groups below the first "cluster" one by one while holding ctrl, which I do since years and takes so much unnecessary time.


----------



## Uiroo (Jan 3, 2023)

I don't think so. I handle it by naming my groups in such a way that I can use filters to select certain types of groups:


----------



## Lionel Schmitt (Jan 3, 2023)

Uiroo said:


> I don't think so. I handle it by naming my groups in such a way that I can use filters to select certain types of groups:


Thanks, yea makes sense. Unfortunately I can't control that when tweaking commercial libraries hah...
Luckily most of the time the naming is alright.


----------



## EvilDragon (Jan 5, 2023)

Yes it's possible. You use Ctrl+click to select a group just like in Explorer. Shift is for contiguous selection. You can also Ctrl+Shift to have a block of groups selected non-contiguously (you first Ctrl+click to select a non-contiguous group, then add Shift to make a block selection, see GIF). After you select the groups, right click and choose "Set edit flag for selected groups".


----------



## Uiroo (Jan 5, 2023)

Ah, how cool, thank you!


----------



## Mike Greene (Jan 5, 2023)

I'm closing this thread, since the thread title "KSP Q&A" is not very helpful for knowing what topic is being currently discussed. It makes it so people who may have general KSP knowledge and want to be helpful, but have no knowledge to offer to the current question, still have to open this thread just to see what the current topic even is. It's annoying, not to mention it wastes even more time since this is a multi-page thread, so you have to click and scroll to even _find_ the current question.

Plus a vague title makes searching more difficult. For instance, if someone a year from now had the same group selection question as Lionel, they might use keywords "groups" and "select." But "groups" and "select" would return a lot of threads, so it would be more helpful if the question were in a thread titled, _"Is there a way to select groups that aren't next to each other?",_ so that person could more easily scan through the thread titles and find the right one.


----------

