# Need to send a controller upon loading of a kontakt preset



## gary0318 (Mar 11, 2018)

Hi. I'm not new to programming (30 year veteran). However, completely new to KSP scripting. I'm finding the info that I have found so far, to be terse at best. 

I would like to accomplish one simple thing. 

I need to send a specific controller to midi out immediately upon loading a preset. 

I found the set_controller parameter, but documentation says don't use it in "init". I don't even know if a script runs immediately upon loading a preset anyway.

Any advice would be greatly appreciated.

The script that I attempted was:

on init
set_controller(60,127)
end on

-----------

I have no idea if there are other required scripting lines required to have a complete script. But, I would need it to run, upon loading the preset.

Thanks,
Gary


----------



## d.healey (Mar 11, 2018)

Put it in the on pgs callback which is automatically triggered after on init.


----------



## gary0318 (Mar 11, 2018)

I presume on init runs immediately upon loading the preset in kontakt?


----------



## gary0318 (Mar 11, 2018)

Í only see a on pgs_changed. is that the one?


----------



## P.N. (Mar 11, 2018)

Hi.
That should work. Otherwise, try:


```
on persistence_changed
    set_controller(60,127)
end on
```


If there's something more specific, you could try adjusting the MIDI CC default (if the midi cc is assigned as an external modulator).

PS: Please tag your code, otherwise ED will not be a happy camper. 

Best regards,

Paulo


----------



## gary0318 (Mar 11, 2018)

Thanks for the advice on tagging the code.

Actually, I'm having an issue I don't understand. I am putting the script in an empty slot, naming it, applying it, saving it. And, then saving the preset from komplete kontrol as well. When I reload the user preset from Komplete Kontrol, my preset slot is empty again. Applying does not seem to send the controller... Loading definetely doesn't because the script is gone LOL. Any ideas?


----------



## P.N. (Mar 11, 2018)

Komplete Kontrol issues are not something i can help with...
Hopefuly someone with experience with it will chip in.

Best of luck,

Paulo


----------



## polypx (Mar 11, 2018)

If you add a script to the NKI, you need to save the NKI I think, not just the "preset" (by which I assume you mean Snapshot).

Putting your send_controller in the "persistence changed" method is preferable if you want it to happen when recalling Snapshots.


----------



## gary0318 (Mar 16, 2018)

Sorry I took so long. I had to take out a RAIDED 970gb System driver and replace it with a 4TB RAID0. Full of snags along the way, but up and running again, without having to reinstall everything from scratch. What a relief!

So, I tried all of the above. The script just does not execute upon loading. If I go into the edit mode and hit apply, it executes immediately. But it isn't loading as it should. Whether run from "on init" or "on persistence_changed".

I switched to setting colors on keys for the moment. When I get this working I'll get back to the controller change.

Here is the script:

```
function setcolors

    set_key_color(36,$KEY_COLOR_RED)
    set_key_color(37,$KEY_COLOR_GREEN)
    set_key_color(38,$KEY_COLOR_GREEN)
    set_key_color(39,$KEY_COLOR_GREEN)
    set_key_color(40,$KEY_COLOR_GREEN)
    set_key_color(41,$KEY_COLOR_GREEN)
    set_key_color(42,$KEY_COLOR_GREEN)
    set_key_color(43,$KEY_COLOR_GREEN)
    set_key_color(44,$KEY_COLOR_GREEN)

    set_key_color(48,$KEY_COLOR_RED)
    set_key_color(49,$KEY_COLOR_PURPLE)
    set_key_color(50,$KEY_COLOR_PURPLE)
    set_key_color(51,$KEY_COLOR_PURPLE)
    set_key_color(52,$KEY_COLOR_PURPLE)
    set_key_color(53,$KEY_COLOR_PURPLE)
    set_key_color(54,$KEY_COLOR_PURPLE)
    set_key_color(55,$KEY_COLOR_PURPLE)
    set_key_color(56,$KEY_COLOR_PURPLE)

    set_key_color(60,$KEY_COLOR_RED)
    set_key_color(61,$KEY_COLOR_LIME)
    set_key_color(62,$KEY_COLOR_LIME)
    set_key_color(63,$KEY_COLOR_LIME)
    set_key_color(64,$KEY_COLOR_LIME)
    set_key_color(65,$KEY_COLOR_LIME)
    set_key_color(66,$KEY_COLOR_LIME)
    set_key_color(67,$KEY_COLOR_LIME)
    set_key_color(68,$KEY_COLOR_LIME)

    set_key_color(72,$KEY_COLOR_RED)
    set_key_color(73,$KEY_COLOR_BLUE)
    set_key_color(74,$KEY_COLOR_BLUE)
    set_key_color(75,$KEY_COLOR_BLUE)
    set_key_color(76,$KEY_COLOR_BLUE)
    set_key_color(77,$KEY_COLOR_BLUE)
    set_key_color(78,$KEY_COLOR_BLUE)
    set_key_color(79,$KEY_COLOR_BLUE)
    set_key_color(80,$KEY_COLOR_BLUE)

    set_key_color(84,$KEY_COLOR_RED)
    set_key_color(85,$KEY_COLOR_GREEN)
    set_key_color(86,$KEY_COLOR_CYAN)
    set_key_color(87,$KEY_COLOR_CYAN)
    set_key_color(88,$KEY_COLOR_CYAN)
    set_key_color(89,$KEY_COLOR_CYAN)
    set_key_color(90,$KEY_COLOR_CYAN)
    set_key_color(91,$KEY_COLOR_CYAN)
    set_key_color(92,$KEY_COLOR_CYAN)

end function

on persistence_changed
    call setcolors
end on
```


----------



## EvilDragon (Mar 16, 2018)

You should use a bit of while loops to make that code tidier. 


That said, I have no problems in running set_controller() from persistence_changed... it works and can be verified in the MIDI monitor.


----------



## gary0318 (Mar 16, 2018)

The key colors don't change upon loading the preset. Not until I hit apply on the script itself. I believe I'm having a broader problem... not seeing the forest for the trees. But, I don't know what might stop the execution of the script.


----------



## EvilDragon (Mar 16, 2018)

That's kinda hard to debug when you don't have the whole code... Just the above code you put here definitely works fine here.


----------



## gary0318 (Mar 16, 2018)

I loaded the midi-monitor in the last slot. Did a slight edit to the my script just to make the apply button light up. I hit the apply button. It does not register on the midi monitor. Not surprising, because the key colors aren't changing either.


----------



## gary0318 (Mar 16, 2018)

EvilDragon said:


> That's kinda hard to debug when you don't have the whole code... Just the above code you put here definitely works fine here.


This is the whole code at this point. I haven't added anything else. Is there some *structure* required? the script compiles with just the lines I posted. I assume that it is a working script because of that.


----------



## EvilDragon (Mar 16, 2018)

Yeah the structure is fine. If you want to be absolutely certain, though, add this on top:


```
on init
    set_snapshot_mode(1)
    set_key_pressed_support(1)
end on
```

But... which keycolors are we talking about? Because those on Kontakt's virtual keyboard DO get colored! If you're talking about LightGuide, you need to have Kontakt opened from within Komplete Kontrol in order for it to work...


----------



## gary0318 (Mar 16, 2018)

Wait I spoke in haste about the midi monitor. It is, in fact registering the controller.

So, only issue is trying to get this script to run the code when preset is loaded.


----------



## gary0318 (Mar 16, 2018)

EvilDragon said:


> Yeah the structure is fine. If you want to be absolutely certain, though, add this on top:
> 
> 
> ```
> ...


Just worried about the kontakt color coding at the moment. I don't have my Kontrol 61 here. I'm in Ecuador and it's in the U.S. :( I hope to get it here soon.

I'll try this code now.


----------



## gary0318 (Mar 16, 2018)

gary0318 said:


> Just worried about the kontakt color coding at the moment. I don't have my Kontrol 61 here. I'm in Ecuador and it's in the U.S. :( I hope to get it here soon.
> 
> I'll try this code now.


set_snapshot_mode doesn't seem to exist in the KSP manual and it highlights as error in read when I hit apply.


----------



## gary0318 (Mar 16, 2018)

gary0318 said:


> set_snapshot_mode doesn't seem to exist in the KSP manual and it highlights as error in read when I hit apply.


You mean set_snapshot_type I presume?


----------



## EvilDragon (Mar 16, 2018)

Yeah, that, sorry.


----------



## gary0318 (Mar 16, 2018)

EvilDragon said:


> Yeah, that, sorry.


Is there possibly some parameter within Kontakt or the Instrument to which I am adding the script, that might be inhibiting the script from auto loading?


----------



## gary0318 (Mar 16, 2018)

EvilDragon said:


> Yeah, that, sorry.



Is there possible some parameter in Kontakt or the instrument I am adding the script to, that could be inhibiting the running of the script?


----------



## EvilDragon (Mar 16, 2018)

Nope. That script works here...


----------

