function ToggleVariable(name, a, b)
-- Set str to b if #name# is a, or to a otherwise
local str
if SKIN:GetVariable(name) == a then
str = b
else
str = a
end
-- Set #name# to str
SKIN:Bang('!SetVariable "' .. name .. '" "' .. str .. '"')
end
function ToggleVariable(name, a, b)
SKIN:Bang('!SetVariable "' .. name .. '" "' .. (SKIN:GetVariable(name)==a and b or a) .. '"')
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
poiru wrote:
Yep, that does indeed work and is better than my code (edited first post to point to it). Thanks!
I both agree and disagree. The "shorthand" approach is nice, and I recommend it. Once you get used to it it does make tighter and more efficient code. However, the result is the same and I could argue that the If then else approach is more "accessible" to a user not steeped in programming in languages like C or Java when you are demonstrating something. Any "efficiencies" achieved by using the shorthand approach are going to be completely negligible in a few lines of code.
if string.upper(StartingUnit) == "F" then
if CurrentUnit == "F" then
CurrentUnit = "C"
ConvertedTemp = round((5/9)*(ConvertedTemp-32))
SKIN:Bang("!SetVariable TempColor "..CColor)
SKIN:Bang("!SetVariable TempText "..ConvertedTemp.."°"..CurrentUnit)
else
CurrentUnit = "F"
SKIN:Bang("!SetVariable TempColor "..FColor)
SKIN:Bang("!SetVariable TempText "..ConvertedTemp.."°"..CurrentUnit)
end
elseif string.upper(StartingUnit) == "C" then
if CurrentUnit == "C" then
CurrentUnit = "F"
ConvertedTemp = round((9/5)*ConvertedTemp+32)
SKIN:Bang("!SetVariable TempColor "..FColor)
SKIN:Bang("!SetVariable TempText "..ConvertedTemp.."°"..CurrentUnit)
else
CurrentUnit = "C"
SKIN:Bang("!SetVariable TempColor "..CColor)
SKIN:Bang("!SetVariable TempText "..ConvertedTemp.."°"..CurrentUnit)
end
end
You do get ConvertedTemp from a reliable source each time before this part of the code is run, right? Otherwise I imagine you'd get a hell of a lot of roundoff errors when it's converted back and forth many times.
MerlinTheRed wrote:You do get ConvertedTemp from a reliable source each time before this part of the code is run, right? Otherwise I imagine you'd get a hell of a lot of roundoff errors when it's converted back and forth many times.
ConvertedTemp is retrieved as a whole number from the skin on each Update() and the Lua does not update that number with any converted values. The value is never "rounded" twice.