How to deselect all other buttons when pressing one button?

duskrobot
duskrobot Member Posts: 5 Member
edited January 2022 in Scripting Workshop

Hello,

I have ten buttons that each allows specific groups and disallow others. This works ok, but I'm having an issue with all the other buttons not being deselected when selecting one of them.

I researched the old forum and found this: https://www.native-instruments.com/forum/threads/button-menu-selects-groups.354375/#post-1773979

Unfortunately, it's not working properly for me. Here's the code I have (all the buttons and their IDs have been previously declared):


on init
  declare const $NUM_BUTTONS := 10

  declare $i
  declare $old_btn
  declare $sel_btn
  declare %ID[$NUM_BUTTONS]

  while ($i < $NUM_BUTTONS)
    %ID[$i] := get_ui_id($LongSulTasto) + $i
    inc($i)
  end while

  make_persistent($sel_btn)

end on

function ButtonCB()
  set_control_par(%ID[$old_btn], $CONTROL_PAR_VALUE, 0)
  set_control_par(%ID[$sel_btn], $CONTROL_PAR_VALUE, 1)
  $old_btn := $sel_btn
end function

on ui_control ($LongSulTasto)
  $sel_btn := 0
  call ButtonCB()
end on

on ui_control ($LongTremolo)
  $sel_btn := 1
  call ButtonCB()
end on

on ui_control ($Staccato)
  $sel_btn := 2
  call ButtonCB()
end on

on ui_control ($GlissDown)
  $sel_btn := 3
  call ButtonCB()
end on

on ui_control ($GlissHarm)
  $sel_btn := 4
  call ButtonCB()
end on

on ui_control ($TremHarm)
  $sel_btn := 5
  call ButtonCB()
end on

on ui_control ($CircularBow)
  $sel_btn := 6
  call ButtonCB()
end on

on ui_control ($Crunches)
  $sel_btn := 7
  call ButtonCB()
end on

on ui_control ($ColLegno)
  $sel_btn := 8
  call ButtonCB()
end on

on ui_control ($BodyTaps)
  $sel_btn := 9
  call ButtonCB()
end on

on persistence_changed
  call ButtonCB()
end on


Any help would be greatly appreciated. Thanks!

Best Answer

  • EvilDragon
    EvilDragon Moderator Posts: 1,022 mod
    Answer ✓

    Yeah, that is about IDs. You need to declare all ui_buttons in a single block, because declaring a normal variable in between declarations of two UI widgets will steal variable IDs from them, so you won't have a contiguous list of IDs for those buttons then.

    So basically remove that $LongSulTasto_id variable declaration, you don't need it.

Answers

  • EvilDragon
    EvilDragon Moderator Posts: 1,022 mod
    edited January 2022

    Please use the Code Block paragraph type when posting code. :)

    It works just fine over here...

  • duskrobot
    duskrobot Member Posts: 5 Member

    Thanks E.D.! I just saw the formatting option, sorry about that.

    I'll have to check the rest of the stuff over here. For some reason the buttons are acting up very weirdly.

    Thanks again!

  • duskrobot
    duskrobot Member Posts: 5 Member

    Yeah, this is super weird. I checked the PNG files and the TXT files and all seems to be ok.

    The current behaviour is:

    I click the first button, everything is ok. I click it again and it remains on.

    I click the second one, all ok except if I click it again it deselects itself.

    I click the third one, and that one and the second one are selected. If I click it again it deselects itself.

    I click the fourth one and that one and the third one are selected, and if I click it again it deselects itself.

    Then I click the fifth one and that one, the fourth and the third are selected, and if I click it again it deselects itself.

    It gets weirder after that. It's kind of driving me nuts.

  • EvilDragon
    EvilDragon Moderator Posts: 1,022 mod

    Yeah, no idea. If I just take your code verbatim and add the necessary buttons, it works just fine over here...

  • duskrobot
    duskrobot Member Posts: 5 Member

    I managed to isolate the issue but I can't figure it out. I added the code for the first custom button image to your code and the button declarations, and was able to reproduce the issue, which has something to do with the IDs.

    on init
      make_perfview
      set_ui_height_px(350)
      set_ui_width_px(633)
    
      declare ui_button $LongSulTasto
      declare $LongSulTasto_id
      $LongSulTasto_id:= get_ui_id ($LongSulTasto)
      set_text ($LongSulTasto,"")
      set_control_par_str($LongSulTasto, $CONTROL_PAR_PICTURE, "Button_LongSulTasto")
    
      declare ui_button $LongTremolo
      declare ui_button $Staccato
      declare ui_button $GlissDown
      declare ui_button $GlissHarm
      declare ui_button $TremHarm
      declare ui_button $CircularBow
      declare ui_button $Crunches
      declare ui_button $ColLegno
      declare ui_button $BodyTaps
    
      declare const $NUM_BUTTONS := 10
    
      declare $i
      declare $old_btn
      declare $sel_btn
      declare %ID[$NUM_BUTTONS]
    
      while ($i < $NUM_BUTTONS)
      %ID[$i] := get_ui_id($LongSulTasto) + $i
      inc($i)
      end while
    
      make_persistent($sel_btn)
    
    end on
    
    function ButtonCB()
      set_control_par(%ID[$old_btn], $CONTROL_PAR_VALUE, 0)
      set_control_par(%ID[$sel_btn], $CONTROL_PAR_VALUE, 1)
      $old_btn := $sel_btn
    end function
    
    on ui_control ($LongSulTasto)
      $sel_btn := 0
      call ButtonCB()
    end on
    
    on ui_control ($LongTremolo)
      $sel_btn := 1
      call ButtonCB()
    end on
    
    on ui_control ($Staccato)
      $sel_btn := 2
      call ButtonCB()
    end on
    
    on ui_control ($GlissDown)
      $sel_btn := 3
      call ButtonCB()
    end on
    
    on ui_control ($GlissHarm)
      $sel_btn := 4
      call ButtonCB()
    end on
    
    on ui_control ($TremHarm)
      $sel_btn := 5
      call ButtonCB()
    end on
    
    on ui_control ($CircularBow)
      $sel_btn := 6
      call ButtonCB()
    end on
    
    on ui_control ($Crunches)
      $sel_btn := 7
      call ButtonCB()
    end on
    
    on ui_control ($ColLegno)
      $sel_btn := 8
      call ButtonCB()
    end on
    
    on ui_control ($BodyTaps)
      $sel_btn := 9
      call ButtonCB()
    end on
    
    on persistence_changed
      call ButtonCB()
    end on
    
    
    


  • EvilDragon
    EvilDragon Moderator Posts: 1,022 mod
    Answer ✓

    Yeah, that is about IDs. You need to declare all ui_buttons in a single block, because declaring a normal variable in between declarations of two UI widgets will steal variable IDs from them, so you won't have a contiguous list of IDs for those buttons then.

    So basically remove that $LongSulTasto_id variable declaration, you don't need it.

  • duskrobot
    duskrobot Member Posts: 5 Member

    Thank you so much Evil Dragon! Declaring all the buttons together solved the issue.

    I still had to leave the IDs in or my custom button graphics would be replaced by the generic button graphics on Kontakt.

    Thanks again!

Back To Top