It is currently April 26th, 2024, 12:24 pm

Toggling a (string) variable with Lua

Discuss the use of Lua in Script measures.
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Toggling a (string) variable with Lua

Post by poiru »

Here is a simple method to toggle between two strings (e.g. "show" and "hide") and a demonstration of using !CommandMeasure with Lua:

Skin:

Code: Select all

[Variables]
state=hide

[mLua]
Measure=Script
ScriptFile=Test.lua

...

LeftMouseUpAction=!Execute [!CommandMeasure "mLua" "ToggleVariable('state', 'hide', 'show')"]
Lua:

Code: Select all

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
(For a more compact and faster version, use this)

The example above will toggle #state# between hide and show.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Toggling a (string) variable with Lua

Post by smurfier »

I'm not in a position to test, but I used something similar in LuaCalendar.

Code: Select all

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 . . .
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Toggling a (string) variable with Lua

Post by jsmorley »

I use a similar approach with a weather skin that toggles the display from Fahrenheit to Celsius every 10 seconds...

Code: Select all

	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
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Toggling a (string) variable with Lua

Post by poiru »

smurfier wrote:I'm not in a position to test, but I used something similar in LuaCalendar.

Code: Select all

function ToggleVariable(name, a, b)
   SKIN:Bang('!SetVariable "' .. name .. '" "' .. (SKIN:GetVariable(name)==a and b or a) .. '"')
end
Yep, that does indeed work and is better in terms of performance (edited first post to point to it). Thanks! :)
jsmorley wrote:I use a similar approach with a weather skin that toggles the display from Fahrenheit to Celsius every 10 seconds...
Yeah, a rather basic example, but perfect for showing how !CommandMeasure works with the Script measure.
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Toggling a (string) variable with Lua

Post by jsmorley »

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.
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: Toggling a (string) variable with Lua

Post by MerlinTheRed »

jsmorley wrote:I use a similar approach with a weather skin that toggles the display from Fahrenheit to Celsius every 10 seconds...

Code: Select all

	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.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Toggling a (string) variable with Lua

Post by jsmorley »

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.