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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.