# Help with Kontakt Groups Menu [SOLVED]



## Claud9 (Nov 23, 2016)

Hello everybody,
I have a 2 layer instrument : 
Layer 1 : Piano
Layer 2 : Pads
In the instrument I have created several groups with different pianos and pads sounds.
I want to script a menu for each layer that let me choose between the different piano groups (for Layer 1) and the different Pads groups (for Layer 2).

I have this script, but this create a menu for all Groups , how I can modify it in order to work with my instrument and have two menus, one for Pianos groups and one for Pads Groups? Thanks in advance for any help!

on init
declare ui_menu $selected_group
declare $i
$i := 0
while ($i <= $NUM_GROUPS-1)
add_menu_item($selected_group, group_name($i), $i)
inc($i)
end while
end on

on note
disallow_group($ALL_GROUPS) 
allow_group($selected_group) 
end on


----------



## EvilDragon (Nov 23, 2016)

Don't loop through all groups, loop only through whichever groups you need.


----------



## Claud9 (Nov 23, 2016)

EvilDragon said:


> Don't loop through all groups, loop only through whichever groups you need.


Thanks that is exactly what Ii was thinking. Is there somebody that can make some written example or address me to a link where i can see some written code examples? Thanks in advance for any help!


----------



## Lindon (Nov 24, 2016)

something like this:

```
on init

  declare ui_menu $piano_layer
  declare ui_menu $pad_layer
  declare $piano_group_start := 0
  declare $piano_group_end := 19
  declare $pad_group_start := 20
  declare $pad_group_end := 39
  declare $idx

  $idx := $piano_group_start
  while ($idx<=$piano_group_end)
    add_menu_item($piano_layer,group_name($idx),$idx)
    inc($idx)
  end while

  $idx := $pad_group_start
  while ($idx<=$pad_group_end)
    add_menu_item($pad_layer,group_name($idx),$idx)
    inc($idx)
  end while
end on

on note
  disallow_group($ALL_GROUPS)
  allow_group($piano_layer)
  allow_group($pad_layer)
end on
```

Modify the contents of the start and end variables to match the number of piano and pad groups you have


----------



## EvilDragon (Nov 24, 2016)

You already have it. Don't use $NUM_GROUPS, use group ID of the last group you want to include in the menu. Change the starting point of the loop ($i) to the first group you want.


----------



## Claud9 (Nov 24, 2016)

Lindon said:


> something like this:
> 
> ```
> on init
> ...



Thanks a lot for the help! I will have several Groups, is there a way not to have kontakt to load all the groups and to purge the groups I'm not using?


----------



## EvilDragon (Nov 24, 2016)

Of course it's possible, see KSP reference about purge_group() command.


----------



## Claud9 (Nov 24, 2016)

EvilDragon said:


> Of course it's possible, see KSP reference about purge_group() command.


Thanks for the help, I have elaborated the initial code with your suggestions, Is ok like this?
Where I have to add the "purge " command?
Thanks!


on init
declare ui_menu $piano_group
declare ui_menu $pad_group
declare $i
$i := 0
while ($i <= 1)
add_menu_item($piano_group, group_name($i), $i)
inc($i)
end while
$i := 2
while ($i <= 3)
add_menu_item($pad_group, group_name($i), $i)
inc($i)
end while
end on

on note
disallow_group($ALL_GROUPS)
allow_group($piano_group)
allow_group($pad_group) 
end on


----------



## EvilDragon (Nov 24, 2016)

In the UI callback of each menu, of course. You gotta do it in a loop as well, to purge all unneeded groups, then unpurge the ones you really need.


----------



## Claud9 (Nov 24, 2016)

EvilDragon said:


> In the UI callback of each menu, of course. You gotta do it in a loop as well, to purge all unneeded groups, then unpurge the ones you really need.


thanks but It looks complicated to me, also in KSP menu there is only an example if you have created a "purge" button, I have not seen any similar examples related to my situation.. Thanks in advance for any additional help!


----------



## EvilDragon (Nov 24, 2016)

Menu is pretty much the same thing as a button. Just works differently. The callback is the same. You have all the ingredients to make it work, it's as easy as making a while loop, put in some time to do it yourself, don't just expect finished code to be handed out to you always...


----------



## Claud9 (Nov 24, 2016)

EvilDragon said:


> Menu is pretty much the same thing as a button. Just works differently. The callback is the same. You have all the ingredients to make it work, it's as easy as making a while loop, put in some time to do it yourself, don't just expect finished code to be handed out to you always...



I have this that purge the groups, but is not clear how to unpurge the group i have selected in the menu..
I know that changing the "mode" from 0 to 1 will load those samples, but how can I specify to load the samples of the group I have selected in the menu? 

on init
declare ui_menu $piano_group
declare ui_menu $pad_group
declare $i
$i := 0
while ($i <= 1)
add_menu_item($piano_group, group_name($i), $i)
inc($i)
end while
$i := 2
while ($i <= 3)
add_menu_item($pad_group, group_name($i), $i)
inc($i)
end while
end on

on ui_control ($piano_group)
$i := 0
while ($i <= 1)
purge_group(0,0)
purge_group(1,0)
end while
end on

on ui_control ($pad_group)
$i := 2
while ($i <= 3)
purge_group(2,0)
purge_group(3,0)
end while
end on
on note
disallow_group($ALL_GROUPS)
allow_group($piano_group)
allow_group($pad_group)


----------



## EvilDragon (Nov 24, 2016)

See how the menus were populated with add_menu_item. Do the same thing for purging the groups (mode 0). Then after the loop just unpurge (mode 1) the one group that the menu selected.

Your loops for purging arenot doing anything meaningful right now.


----------



## Claud9 (Nov 24, 2016)

EvilDragon said:


> See how the menus were populated with add_menu_item. Do the same thing for purging the groups (mode 0). Then after the loop just unpurge (mode 1) the one group that the menu selected.
> 
> Your loops for purging arenot doing anything meaningful right now.



Thanks. This code I did is working but It is a mystery to me why "piano_group" works great and instead in order to make it work properly in "pad_group" I had to add "*purge_group (3,0)" *or group 4 stay unpurged...
Any idea why? Thanks in advance for any suggestion

on init

declare ui_menu $piano_group
declare ui_menu $pad_group
declare $i
$i := 0
while ($i <= 1)
add_menu_item($piano_group, group_name($i), $i)
inc($i)
end while
$i := 2
while ($i <= 3)
add_menu_item($pad_group, group_name($i), $i)
inc($i)
end while
end on


on ui_control ($piano_group)
if ($piano_group=0)
purge_group (0,1)
else
purge_group (0,0)

if ($piano_group=1)
purge_group (1,1)
else
purge_group (1,0)

end if
end if
end on

on ui_control ($pad_group)
if ($pad_group=2)
purge_group (2,1)
*purge_group (3,0)*
else
purge_group (2,0)

if ($pad_group=3)
purge_group (3,1)
else
purge_group (3,0)

end if
end if
end on


on note
disallow_group($ALL_GROUPS)
allow_group($piano_group)
allow_group($pad_group) 
end on


----------



## Claud9 (Nov 24, 2016)

EvilDragon said:


> See how the menus were populated with add_menu_item. Do the same thing for purging the groups (mode 0). Then after the loop just unpurge (mode 1) the one group that the menu selected.
> 
> Your loops for purging arenot doing anything meaningful right now.



Ok this looks to work fine, can somebody confirm me that is correct before I start coding all the groups?
Thanks in advance !

on init

declare ui_menu $piano_group
declare ui_menu $pad_group
declare $i
$i := 0
while ($i <= 1)
add_menu_item($piano_group, group_name($i), $i)
inc($i)
end while
$i := 2
while ($i <= 3)
add_menu_item($pad_group, group_name($i), $i)
inc($i)
end while
end on


on ui_control ($piano_group)
if ($piano_group=0)
purge_group (0,1)
purge_group (1,0)
else
purge_group (0,0)

if ($piano_group=1)
purge_group (1,1)
purge_group (0,0)
else
purge_group (1,0)
end if
end if
end on

on ui_control ($pad_group)
if ($pad_group=2)
purge_group (2,1)
purge_group (3,0)
else
purge_group (2,0)

if ($pad_group=3)
purge_group (3,1)
purge_group (2,0)
else
purge_group (3,0)
end if
end if
end on


on note
disallow_group($ALL_GROUPS)
allow_group($piano_group)
allow_group($pad_group) 
end on


----------



## EvilDragon (Nov 24, 2016)

It's wrong (as in: too lengthy for what you're trying to do). Why aren't you using while loops with constants, like how Lindon showed you?

Also, please use CODE tags when pasting code here.

This is how it should be. Simple - your thinking is way too complicated.


```
on init
   declare const $PIANO_FIRST := 0
   declare const $PIANO_LAST := 19
   declare const $PAD_FIRST := 20
   declare const $PAD_LAST := 39

   declare $i

   declare ui_menu $piano_layer
   declare ui_menu $pad_layer

   $i := $PIANO_FIRST
   while ($i<=$PIANO_LAST)
     add_menu_item($piano_layer,group_name($i),$i)
     inc($i)
   end while

   $i:= $PAD_FIRST
   while ($i<=$PAD_LAST)
     add_menu_item($pad_layer,group_name($i),$i)
     inc($i)
   end while
end on

on ui_control ($piano_layer)
   $i := $PIANO_FIRST
   while ($i<=$PIANO_LAST)
     purge_group($i,0)
     inc($i)
   end while
   purge_group($piano_layer,1)
end on

on ui_control ($pad_layer)
   $i := $PAD_FIRST
   while ($i<=$PAD_LAST)
     purge_group($i,0)
     inc($i)
   end while
   purge_group($pad_layer,1)
end on
```

If you're only selecting one group, you don't even need group allow/disallow in the note callback, since purging basically does that for you.


----------



## Aviram Dayan Production (Nov 25, 2016)

The best way to understand the "purge":
if mode on 0 is purge/disallow | if mode on 1 is unpurge/allow
It solves a lot of questions


----------



## Claud9 (Nov 25, 2016)

EvilDragon said:


> It's wrong (as in: too lengthy for what you're trying to do). Why aren't you using while loops with constants, like how Lindon showed you?
> 
> Also, please use CODE tags when pasting code here.
> 
> ...


Thanks a lot, this is a lot better than what I found by myself, I will try to think in a less complicated way next time!


----------



## Claud9 (Nov 25, 2016)

Aviram Dayan Production said:


> The best way to understand the "purge":
> if mode on 0 is purge/disallow | if mode on 1 is unpurge/allow
> It solves a lot of questions


Thanks for the tip Aviram, but that was the only thing already 100% clear to me.


----------

