4
\$\begingroup\$

Is there a more elegant way to set this boolean value? SetScript() is a game API, while "OnClick" is its handler. What is important is the method that triggers when I click on the checkbutton.

local foo = true

checkbutton:SetScript("OnClick", function()
   if foo == true then foo = false
   else foo = true
   end
end)
\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

a more elegant way ?

Simply assign the negation, unconditionally: not foo

This works in Lua, and in essentially every other language.

checkbutton:SetScript("OnClick", function()
   foo = not foo
end)

If we're flipping {0, 1} integers instead of booleans, then the relevant idiom is to assign 1 - foo.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.