Skip to content

Radio Buttons without Infinite Looping

John Goodland edited this page Sep 2, 2022 · 2 revisions

Here is code for radio buttons using source to avoid infinite loops.

Table t{} contains the names of the buttons in the radio group:


ProgramSelect = function(mod,value,source)
    if source == 4 then
        local sName = L(mod:getName())
        local t = {"PROGRAM01", "PROGRAM02", "PROGRAM03", "PROGRAM04"} -- local table of 4 radio buttons
        for i, v in ipairs(t) do
            if sName == v then
                local sysMess = string.format("F0 00 00 23 01 43 C1 00 %.2X F7", i - 1)
                panel:sendMidiMessageNow(CtrlrMidiMessage(sysMess))
            else
                _G[v]:setValue(0, true) 
               -- don't use panel:getComponent(v) // use panel:getModulatorByName(v)
            end
        end
    end
end -- source == 4 (user clicked on a button - not lua generated - that would be 5 or 6) end

--[[ 
*NOTE: Where PROGRAM01 etc are initialised in an init script
"Called when the panel has finished loading"
PROGRAM01=panel:getModulatorByName("PROGRAM01")
PROGRAM02=panel:getModulatorByName("PROGRAM02")
PROGRAM03=panel:getModulatorByName("PROGRAM03")
PROGRAM04=panel:getModulatorByName("PROGRAM04")
--]]

https://ctrlr.org/radio-button-code-generator/