How to toggle - The inner workings of the switch

params.color = color

switchState(buttonIndex)

function redAISwitch() switchState("Red AI Toggle") end function greenAISwitch() switchState("Green AI Toggle") end function blueAISwitch() switchState("Blue AI Toggle") end function yellowAISwitch() switchState("Yellow AI Toggle") end

toggleSwitches[index] = not toggleSwitches[index]

function updateInterface(index) print("Method started on "..self.getName()) local btns = self.getButtons() for i, btn in pairs(btns) do if toggleSwitches[btn.label] then self.editButton({ index = btn.index, color = toggleColors[2] }) elseif string.find(btn.label, "Toggle") ~= nil then self.editButton({ index = btn.index, color = toggleColors[1] }) end end end

At first we aquire a table of all buttons by calling self.getButtons(). Be aware that if you do not use this as an object script, you have to call this method on the actual object and not self



We then use a for loop to loop over all buttons in the list. Its state is checked by using its label as a key for our state table. Because we use bool values, a if else clause can be directly be used for that. Note the check for the word "Toggle" in the elseif clause. This is necessary if you are using normal buttons besides the toggles. Otherwise all buttons would be colored in the off-state color. The word "Toggle" can be replaced by any other word, it is just needed to identify the toggles and distiguish them from the normal buttons.



The actual editing of the button visuals is then performed inside the if-else clause. Here we only need to know the index of the button, which we can aquire easily by calling btn.index. The button is then be colored via setting its color parameter according to its state.

The first step is the creation of the button. Because this is no guide for beginners, i will not go into further details (i you do not know how to do that: Look at this guide The only important thing to remember is, that the button has to be created in the color representing its state.This can be done by setting the color parameter of the button to the respective color. This is straightforward:Now we have to determine what it has to do when it is clicked by setting its click function parameter. Because its a switch, we want it to change its current state.Normally one would assume that you would set the click function to something likeThis, however, is not simply possible because lua does not provide a way to determine which button has called the function, so one has to do a little workaround by introducing a mediator click function for each button, which will provide the necessary informations:The click function will call our switchState function and provide it with the information it needs to do its job, namely an identifier for the button.Now that we know what button to toggle, we need to change its state:The only thing left to do now is to update the visual representation of the button to reflect its changed state. This is where it get's a little trickier. A convinient method is to simply loop over all buttons a object has and change the color parameter of each one according to its state. If you use booleans, this state can simply be checked by an if else clause: