Multi Group Purge With Menu

MTC84
MTC84 Member Posts: 71 Member
edited October 2024 in Scripting Workshop

Hi,

I have an instrument with 5 groups per Menu item/preset. I want to purge the unused groups when selecting from the menu but can't seem to figure out where I am going wrong.

Can anybody please help? Do I need to add a Fade_Out somewhere also?

I will add Previous + Next buttons later in case that matters at this stage.

This a test using 10 groups for now to simulate 2 banks of 5 groups.

Thanks very much.

on init
make_perfview
set_script_title("MIXER")
declare const $UI_HEIGHT := 120
set_ui_height_px(120) set_skin_offset(0)
message ("")


declare const $MENU_A_FIRST := 0
declare const $MENU_A_LAST := 9 {PRESET MENU} declare $GROUP_COUNT
$GROUP_COUNT := $NUM_GROUPS


declare ui_menu $MENU_A

move_control_px($MENU_A, 138, 56)
set_control_par(get_ui_id($MENU_A), $CONTROL_PAR_WIDTH, 89)
add_menu_item ($MENU_A, "01", 0)
add_menu_item ($MENU_A, "01 - REVERSED", 5)


make_persistent($MENU_A)
read_persistent_var ($MENU_A) end on
{ON UI CONTROLS - MENU A} on ui_control ($MENU_A) $GROUP_COUNT := 0
while ($GROUP_COUNT <= $MENU_A_LAST)
purge_group($GROUP_COUNT, 0)
purge_group($GROUP_COUNT +1, 0) {***This method isn't working***}
purge_group($GROUP_COUNT +2, 0)
purge_group($GROUP_COUNT +3, 0)
purge_group($GROUP_COUNT +4, 0)
inc($GROUP_COUNT)
end while

purge_group($MENU_A, 1)
purge_group($MENU_A +1, 1) {***This method isn't working***}
purge_group($MENU_A +2, 1)
purge_group($MENU_A +3, 1)
purge_group($MENU_A +4, 1) end on {ON NOTE} on note
select ($MENU_A)
case 0
disallow_group ($ALL_GROUPS)
allow_group(0)
allow_group(1)
allow_group(2)
allow_group(3)
allow_group(4) case 5
disallow_group ($ALL_GROUPS)
allow_group(5)
allow_group(6)
allow_group(7)
allow_group(8)
allow_group(9) end select
end on

«1

Comments

  • theodorech
    theodorech Member Posts: 73 Member
    edited June 2024

    To start, you don't need a special variable for the number of groups because KSP already has one (NUM_GROUPS). Basically, you just need a counter for your while loop, like counter or i. Also, use a while loop inside the note callback; otherwise, you'll end up duplicating the allow group code a bunch of times.

    i:= 0
    while (i < 5 - 1)
    allow_group(i)
    inc(i)
    end while
    

    The purge code needs to run inside the menus' callback, or it won't work when you change the sound source. First, use a while loop to purge all groups, and then unpurge (load) the selected group based on the menu choice.

  • MTC84
    MTC84 Member Posts: 71 Member

    Thanks for the reply but unfortunately I'm still struggling with the unpurge code and not sure how to proceed.

    Just focussing on the Menu ui call back code…

    Step 1 - Purging all groups works correctly when tested without the unpurge line of code.

    Step 2 - Unpurging doesn't work as I need.

    The code below only unpurges group 0 + 8 when I select item 1 in the Menu. Selecting Item 2 unpurges groups 1 to 7 and 10..

    Any advice on getting the unpurge code right would be a great help.

    on ui_control ($MENU_A)
    $i := 0 while ($i <= $MENU_A_LAST)
    purge_group($i, 0)
    inc($i)
    end while

    purge_group($MENU_A, 1) {***NOT WORKING PROPERLY***} end on

  • theodorech
    theodorech Member Posts: 73 Member

    It won't work as-is because you'll need to modify the purge code to suit your group structure. For your setup, each sound source (or preset) comprises 4 groups. This means your first sound source includes groups 0 to 3, your second source includes groups 4 to 7, and so on. Initially, you need to set your menu index in a sequential order (0, 1, 2, 3, etc.), and then adjust the purge group accordingly.

    $i := 0
    while ($i < 4)
    purge_group($my_menu * 4 + $i, 1)
    inc($i)
    end while

    In the code above, you first loop through 4 groups, which constitute your sound source. Then, you use the menu index, multiplying it by 4 because every 4 groups correspond to a new sound source. By using the counter ($i), you can load the 4 groups that form your preset based on the menu selection.

  • MTC84
    MTC84 Member Posts: 71 Member

    Thanks for explaining the code as you did. Very helpful for me and others who see this in future.

    I have tried your code but it's still not working for me. Any ideas?

    Here is the whole test code in case I have made an error elsewhere.

    Thanks for your help.

    on init
    make_perfview
    set_script_title("MIXER")
    declare const $UI_HEIGHT := 160
    set_ui_height_px(160)


    set_skin_offset(0)
    message ("")

    declare const $MENU_A_FIRST := 0
    declare const $MENU_A_LAST := 9 {SAMPLE SELECT / PRESET MENUS} declare $i
    $i := $NUM_GROUPS {PRESET MENU CONTROLS} declare ui_menu $MENU_A

    move_control_px($MENU_A, 538, 86)
    set_control_par(get_ui_id($MENU_A), $CONTROL_PAR_WIDTH, 89)
    add_menu_item ($MENU_A, "01", 0)
    add_menu_item ($MENU_A, "02", 5)

    make_persistent($MENU_A)
    read_persistent_var ($MENU_A) end on
    {ON UI CONTROLS - MENU A} on ui_control ($MENU_A) {PURGE ALL} $i := 0

    while ($i <= $MENU_A_LAST)
    purge_group($i, 0)
    inc($i)
    end while {GROUP ALLOW - NOT WORKING} $i := 0

    while ($i < 5)
    purge_group($MENU_A * 5 + $i, 1)
    inc($i)
    end while end on {ON NOTE} on note
    select ($MENU_A)
    case 0
    disallow_group ($ALL_GROUPS)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4) case 5
    disallow_group ($ALL_GROUPS)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9) end select
    end on

  • theodorech
    theodorech Member Posts: 73 Member

    Check my response carefully :)


    Initially, you need to set your menu index in a sequential order (0, 1, 2, 3, etc.), and then adjust the purge group accordingly.

  • MTC84
    MTC84 Member Posts: 71 Member
    edited June 2024

    I was under the impression I had to use Menu index numbers that relate to the groups I am selecting. Hence the non-sequential numbering.

    I changed the Group Index to 0, 1, 2 etc and updated the on note code too. Now the menu doesn't select the correct groups. It only plays a single group, not all 5.

    Unpurging still isn't working.

    I still can't figure this out. I'm lost. Thanks for your patience.

  • theodorech
    theodorech Member Posts: 73 Member
    edited July 2024

    The code you shared works great once you update the menu index accordingly. For the note callback, it’s better to use a similar while loop as we did for the purge code, instead of using individual lines of allow_group code.

    By the way, this one doesn't make any sense:

    declare $i
    $i := $NUM_GROUPS {remove this line}
    

    Here's the change you need to do in the menu index:

    add_menu_item ($MENU_A, "01", 0)
    add_menu_item ($MENU_A, "02", 1) {1 instead of 5}
    

    I think you're trying to apply the above examples to a much longer code. I'd suggest either studying the examples above and adjusting your full code accordingly or sending the full code so we can troubleshoot it more efficiently.

  • MTC84
    MTC84 Member Posts: 71 Member
    edited July 2024

    Thanks for the reply.

    I tried the sequential Menu numbering but now my menu doesn't work.

    I'm ony building a test UI here to check the purging code - there is no extra code I am not sharing so I am very confused why it is working for you and not me. The only part I haven't yet sorted is the On Note CB. I wanted to get the purge working properly before moving on to that stage.

    I can attach a test instrument if that helps.

    Here is my code in full with your suggested changes. The purge and unpurge code no longer works as I have chhanged the menu indexing as you said to.

    Thanks so much for your time.

    on init
    make_perfview
    set_script_title("MIXER")
    declare const $UI_HEIGHT := 160
    set_ui_height_px(160)
    set_skin_offset(0)
    message ("") declare const $MENU_A_FIRST := 0
    declare const $MENU_A_LAST := 9 {SAMPLE SELECT / PRESET MENUS} declare $i {PRESET MENU CONTROLS} declare ui_menu $MENU_A

    move_control_px($MENU_A, 538, 86)
    set_control_par(get_ui_id($MENU_A), $CONTROL_PAR_WIDTH, 89)
    add_menu_item ($MENU_A, "01", 0)
    add_menu_item ($MENU_A, "02", 1)

    make_persistent($MENU_A)
    read_persistent_var ($MENU_A) end on {SAMPLE MENU CONTROLS}
    {ON UI CONTROLS - MENU A} on ui_control ($MENU_A) {PURGE ALL} $i := 0

    while ($i <= $MENU_A_LAST)
    purge_group($i, 0)
    inc($i)
    end while {GROUP ALLOW} $i := 0

    while ($i < 5)
    purge_group($MENU_A * 5 + $i, 1)
    inc($i)
    end while end on {ON NOTE} on note
    select ($MENU_A)
    case 0
    disallow_group ($ALL_GROUPS)
    allow_group(0)
    allow_group(1)
    allow_group(2)
    allow_group(3)
    allow_group(4) case 1
    disallow_group ($ALL_GROUPS)
    allow_group(5)
    allow_group(6)
    allow_group(7)
    allow_group(8)
    allow_group(9) end select
    end on

  • MTC84
    MTC84 Member Posts: 71 Member

    This is the test instrument in case it helps. I'm not using a cracked Kontakt or anything dodgy. All is official and paid for. :-)

    10 groups total - 5 groups per Menu Item.

  • medusa
    medusa Member Posts: 98 Helper

    The problem is that it's the same sample, so you can't have it purged and unpurged at the same time.

    You'll need to use different samples in the different groups for it to work.

  • MTC84
    MTC84 Member Posts: 71 Member

    No freaking way! Thanks! I will do a quick test now and confirm if it works.

  • MTC84
    MTC84 Member Posts: 71 Member

    It works. Thank you so much for clearing this up! I would have been going around in circles without you 2.

  • MTC84
    MTC84 Member Posts: 71 Member
    edited July 2024

    I have updated the On Note section as suggested in one of the 1st replies = works perfectly.

    I'm now playing around with adding the Prev + Next buttons. Found a few quirks with the code I usually use so will report back. Hopefully with a solution. This thread will no doubt be useful for other folks.

  • MTC84
    MTC84 Member Posts: 71 Member
    edited July 2024

    In order to add a Prev + Next button am I right in thinking I need to use a function CB? I have moved the Menu A code into a Function Menu_Purge CB and now have a code error.

    Thanks.

    {FUNCTION MENU PURGE}
    function MENU_A_PURGE {PURGE ALL} $i := 0

    while ($i <= $MENU_A_LAST)
    purge_group($i, 0)
    inc($i)
    end while {GROUP ALLOW} $i := 0

    while ($i < 5)
    purge_group($MENU_A * 5 + $i, 1) {***ERROR ON THIS LINE***}
    inc($i)
    end while end function

    The code I am using for the Prev Button is below. I am using an on Persistence Changed CB to update the menu with this method as it seemed to make sense but I may be wrong.

    on ui_control($MENU_A_PREV_BUTTON)
    $MENU_A := ($MENU_A + $i - 1) mod $i
    $MENU_A_PREV_BUTTON := 0

    call MENU_A_PURGE end on

  • MTC84
    MTC84 Member Posts: 71 Member
    edited July 2024

    I have gone back to using the method without the Function CB and have everything working.

    I have simply copied the Purge + Unpurge code into the Prev + Next on ui_control code for each button.

    After bringing the test code into a big instrument I have made (800 groups with 20 groups per menu item - 4 Round Robins + 5 mic channels) I've discovered an issue:

    The menu shows all 40 menu items I have added in the menu code but the Prev + Next buttons only cycle through the first 20 menu items. I've checked all of the numbers in the code and they appear correct as per the test instrument I made.

    Any ideas? I tested the same number of groups (800) inside the test instrument and still have the same problem so I know it's not something clashing with the huge code I have written for the proper instrument.

    Thanks so much for all the help.

This discussion has been closed.
Back To Top