1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

I'm looooooping

Discussion in 'KONTAKT: Sampling, Programming & Scripting' started by dfhagai, Jun 8, 2018.

  1. dfhagai

    dfhagai HD

    552
    63
    Aug 24, 2009
    Israel
    Hi,

    For the life of me, I can't get out of this CPU killing, infinite loop.
    Any help would be much appreciated:
    Code:
    on init
        make_perfview
        set_ui_height_px(100)
        message("")
    
        declare ui_value_edit $1st_cc (1, 127, 1)
        declare $index
        
        declare %chosen_ccs[4]
                %chosen_ccs[0] := 10
                %chosen_ccs[1] := 15
                %chosen_ccs[2] := 20
                %chosen_ccs[3] := 30                                   
    end on   
    
    
    on ui_control ($1st_cc)
        $index := search(%chosen_ccs,$1st_cc)
        while (($index # -1) and (in_range($1st_cc, 1, 128))) {As long as the value already exists in the array and it's within 1-128 range...}
            if ($1st_cc > %chosen_ccs[$index]) {If the new CC value is bigger then the new CC}
                inc($1st_cc)
            end if
            if ($1st_cc < %chosen_ccs[$index]) {If the original CC value is bigger then the new CC}
                dec($1st_cc)
            end if 
        end while
    end on
     
  2. EvilDragon

    EvilDragon KSP Wizard

    4,558
    2,360
    May 25, 2010
    Croatia
    I think you should probably use if ($index # -1) before the while loop first.
     
  3. OP
    OP
    dfhagai

    dfhagai HD

    552
    63
    Aug 24, 2009
    Israel
  4. andreasOL

    andreasOL Senior Member

    254
    16
    Jan 9, 2010
    Hesel
    Interesting...

    if $1st_cc is not one of those %chosen_ccs[4] the loop never runs because "$index # -1" as first condition of your while loop is false. So far no endless loop, right?

    If $index is not -1, i.e. $1st_cc has been found in the array it means that "$1st_cc = %chosen_ccs[$index]", so both if clauses are never executed, so nothing changes and the while runs endlessly (until terminated by Kontakt).

    Your second condition of the while "(in_range($1st_cc, 1, 128)" is redundant, as $1st_cc is confined to [1...127] so it's always betwenn 1 and 128 (why 128).

    So back to: what are you trying to achieve?

    (I haven't run this script, just my observations from looking at it)

    best,
    Andreas
     
  5. OP
    OP
    dfhagai

    dfhagai HD

    552
    63
    Aug 24, 2009
    Israel
    Thanks for your reply Andreas, really appreciated!
    The code above is a piece from this full Multi script here. It's the part that blocks the user from selecting the same CC twice.
    It skips the CC if it already exists in the array, and jumps to the next available one.

    Here's the part that I cant make sense of (try it yourself):
    If I use the value edit arrows, it works fine. If I change the value edit by dragging up/down, it explodes....
    Totally baffled...
     
    Last edited: Jun 10, 2018
  6. EvilDragon

    EvilDragon KSP Wizard

    4,558
    2,360
    May 25, 2010
    Croatia
    IMHO the best way for doing what you wanna do is by using ui_menus, then dynamically hiding entries that were selected in other menus.
     
  7. OP
    OP
    dfhagai

    dfhagai HD

    552
    63
    Aug 24, 2009
    Israel
    That would probably work, but I'll stay dumb and it's far less sexy.... :)
    Any idea why clicking the arrows produces stable results while dragging the numbers creates a bug?
     
  8. EvilDragon

    EvilDragon KSP Wizard

    4,558
    2,360
    May 25, 2010
    Croatia
    Because when you click the value edit you call the callback once. When you drag the numbers you call the UI callback MANY times, and with such an unsafe while loop that you have there, it's just not going to fly.
     
  9. OP
    OP
    dfhagai

    dfhagai HD

    552
    63
    Aug 24, 2009
    Israel

Share This Page