Knob value reset to 0

Discussion in 'Scripting Workshop' started by Kooza, Jul 29, 2021.

  1. Kooza

    Kooza NI Product Owner

    Messages:
    22
    Hello there. I have two scripts communicating with some pgs lines.
    Everything is working good, except for one knob, which is the only one that souldn't reset to 0, but it does as soon as I click on any other control that is pgs controlled.
    It does it just the first time, it doesn't if it is actually the first control touched and it goes to defval is cmd-click on init.
    The script is the unison-portamento preset and the knob is mono_mode_knob
     
  2. Kooza

    Kooza NI Product Owner

    Messages:
    22
  3. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    You probably need to set the value of that control through PGS as well.
     
  4. Kooza

    Kooza NI Product Owner

    Messages:
    22
    I get a "clipped to min value" error but I'm not sure what is referring to
     
  5. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    I guess you just need to check exactly what you're doing in pgs_changed callback, if you aren't somewhere inadvertly setting that knob out of range.
     
  6. Kooza

    Kooza NI Product Owner

    Messages:
    22
    My script:
    Code:
    on init
    
    declare ui_knob $mono_mode_knob (0,3,1)
            set_control_help ($mono_mode_knob,"Mono Mode: <On> will always trigger a new sample, <Legato> does not trigger new samples. <Offset> will trigger a new sample with a sample start offset, determined by the length of the previous held note.")
            declare !Mode_name [4]
            !Mode_name[0] := "Off"
            !Mode_name[1] := "On"
            !Mode_name[2] := "Legato"
            !Mode_name[3] := "Offset"
            set_knob_label($mono_mode_knob,!Mode_name[$mono_mode_knob])    set_control_par(get_ui_id($mono_mode_knob),$CONTROL_PAR_DEFAULT_VALUE,1)
                pgs_create_key(MODE, 1)
     set_text ($ModeFont,"Mono Mode")
            set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
     move_control_px ($ModeFont,536,444)
     hide_part ($mono_mode_knob,$HIDE_PART_BG .or. $HIDE_PART_TITLE)
    hide_part ($ModeFont,$HIDE_PART_BG)
    make_persistent ($ModeFont)
     read_persistent_var ($mono_mode_knob)
     read_persistent_var ($ModeFont)
    declare $one_time
      pgs_create_key(PINIT,1)
      pgs_set_key_val(PINIT,0,1)  { trigger pgs for post init }
    
    end on
    
    on ui_control ($mono_mode_knob)
        pgs_set_key_val(MODE, 0, $mono_mode_knob)
        set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
    end on
    
    on pgs_changed
    if (pgs_key_exists(MODE) and pgs_get_key_val(MODE, 0) # $mono_mode_knob)
        $mono_mode_knob := pgs_get_key_val(MODE, 0)
        set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
        end if
    end on
    What I add in preset script:
    Code:
    function MonoMode()
       set_knob_label($mono_mode_knob,!Mode_name[$mono_mode_knob])
       select ($mono_mode_knob)
           case 0
               $mono_button := 0
           case 1
               $mono_button := 1
               $legato_button := 0
           case 2
               $mono_button := 1
               $legato_button := 1
           case 3
               $mono_button := 1
               $legato_button := 0
       end select
       if ($mono_mode_knob # 2 and $mono_amp_rel_button = 0)
           set_text($mono_fade_in_knob,"Fade In")   
           set_text($mono_fade_out_knob,"Fade Out")   
       else
           set_text($mono_fade_in_knob,"n/a")   
           set_text($mono_fade_out_knob,"n/a")   
       end if
       
       if ($mono_mode_knob # 2)
           set_text($mono_amp_rel_label,"Amp Release:")
       else
           set_text($mono_amp_rel_label,"n/a")
       end if
       
       $mono := $mono_button
       $legato := $legato_button
       if ($mono_button = 1 and $mono_key_up_button = 1 and $bad_active = 0)
           $bad_active := 1
           $a := 0
           while($a < 128)   
               %bad_retrigger_id[$a] := %pressed_id[$a]
               inc($a)
           end while
       else
           $bad_active := 0
       end if
    end function
    
    
    on ui_control ($mono_mode_knob)
        pgs_set_key_val(MODE, 0, $mono_mode_knob)
        set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
    end on
    
    on pgs_changed
    if (pgs_key_exists(MODE) and pgs_get_key_val(MODE, 0) # $mono_mode_knob)
         $mono_mode_knob := pgs_get_key_val(MODE, 0)
        set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
        call MonoMode()
        end if
    end on
     
  7. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    That code works here just fine. So I guess the error is somewhere else.

    Also you don't need that PINIT PGS key - we use persistence_changed callback these days instead of that.
     
  8. Kooza

    Kooza NI Product Owner

    Messages:
    22
    Still don't. I mean, both knobs work great, it's just that they init at position 0 despite saving the patch in different position and giving them defval 1 (cmd-click works in fact)
     
  9. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    Setting defval doesn't actually set the knob to that value, it just sets to which value the knob will go when you Ctrl-click it. You can just $mono_mode_knob := 1 in init callback if you want to set it to that position. Make sure it's also made persistent.
     
  10. Kooza

    Kooza NI Product Owner

    Messages:
    22
    That worked for the knob itself, but the label didn't. Knob is in correct position "on", while label still says "off" tho. First click on it, correctly updates the label. First click anywhere else brings the knob back to off (worth mentioning that beside that, synth is monophonic upon start)
     
  11. corbo-billy

    corbo-billy NI Product Owner

    Messages:
    634
    By instinct, I checked this right away; it is well written . ;)
     
  12. Kooza

    Kooza NI Product Owner

    Messages:
    22
    make_persistent ($mono_mode_knob)
    read_persistent_var ($mono_mode_knob)

    Already there in both scripts. Am I missing something with em?
     
  13. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    Ha, dunno then. Try updating the label in persistence_changed?
     
  14. Kooza

    Kooza NI Product Owner

    Messages:
    22
    like this?

    On persistence_changed
    Call !Mode_name
    End on
     
  15. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    Probably no need to call the whole function, just do the label update.
     
  16. Kooza

    Kooza NI Product Owner

    Messages:
    22
    Code:
    on persistence_changed
    set_knob_label ($mono_mode_knob,!Mode_name[$mono_mode_knob])
    end on
    This solved the startup written value, but I'm back to the point when clicking first on another control brings mode knob back to position 0
     
  17. EvilDragon

    EvilDragon Moderator Moderator

    Messages:
    19,513
    Really not possible to suggest anything else without seeing the whole code...