It is currently June 26th, 2019, 12:49 am

Conditional (if/else) may not be working

Discuss the use of Lua in Script measures.
RKC_317
Posts: 2
Joined: May 9th, 2016, 11:14 pm

Conditional (if/else) may not be working

RKC_317 » May 9th, 2016, 11:23 pm

Or rather, I might not be writing the right value to compare.
It first depends on webparser to gather the information to <td> Shop Motion</td> which determines whether the shop is Open or note

Code: Select all

RegExp=(?siU)<td.*>Open Switch</td>[\n]<td>(.*)</td>[\n]<td.*>Front Door</td>[\n]<td>(.*)</td>[\n]<td.*>Main Door</td>[\n]<td>(.*)</td>[\n]<td.*>Office Motion</td>[\n]<td>(.*)</td>[\n]<td.*>Shop Motion</td>[\n]<td>(.*)</td>[\n]<td.*>Temperature</td>[\n]<td>(.*)</td>
Then it uses the following Meters/Measures:

Code: Select all

[MeasureShopMotion]
Measure=Plugin
Plugin=WebParser
URL=[MeasureSite]
StringIndex=5

Code: Select all

[MeasureLuaScript]
Measure=Script
ScriptFile="HAL.lua"
TableName=HAL
MeasureToSetValue=MeasureShopMotion
;UpdateDivider=5

[MeterOpen]
Meter=Image
ImageName=#@#hal_open.png
X=8
Y=12
W=262
H=100
PreserveAspectRatio=1

[MeterClosed]
Meter=Image
ImageName=#@#hal_closed.png
X=8
Y=12
W=262
H=100
PreserveAspectRatio=1

[MeterLuaReturn]
Meter=Image
MeasureName=MeasureLuaScript
X=8
Y=12
W=262
H=100
PreserveAspectRatio=1

[MeterShopMotion]
Meter=String
MeasureName=MeasureShopMotion
X=270
Y=0r
W=250
H=15
FontSize=11
FontColor=252,251,202,255
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
AntiAlias=1


Here is the LUA script:

Code: Select all

function initialize()
	
	mtMeasureToSetValue = SKIN:GetMeasure('MeasureShopMotion')
	
	
	mtOpenMeter = SKIN:GetMeter('MeterOpen')
	mtClosedMeter = SKIN:GetMeter('MeterClosed')
end

function update()
	MeasureShop = mtMeasureToSetValue:GetStringValue()
	
	if MeasureShop == 'Moving' then
	SKIN:Bang('!ShowMeter "MeterOpen"')
	SKIN:Bang('!HideMeter "MeterClosed"')
	else
	SKIN:Bang('!ShowMeter "MeterClosed"')
	SKIN:Bang('!HideMeter "MeterOpen"')
	end 
	
	if MeasureShop == 'Moving' then
	mtOpenMeter:Show()
	mtClosedMeter:Hide()
	else
	mtClosedMeter:Show()
	mtOpenMeter:Hide()
	end 
	
	return MeasureShop
	
end 
What am I doing wrong?
User avatar
ikarus1969
Posts: 327
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Conditional (if/else) may not be working

ikarus1969 » May 10th, 2016, 10:27 am

I would add the following to your "MeasureShopMotion":

Code: Select all

[MeasureShopMotion]
Measure=Plugin
Plugin=WebParser
URL=[MeasureSite]
StringIndex=5
IfMatchMode=1
IfMatch=Moving
IfMatchAction=[!ShowMeter "MeterOpen"][!HideMeter "MeterClosed"][!SetOption "MeterLuaReturn" "Text" "[#CURRENTSECTION#]"]
IfNotMatchAction=[!HideMeter "MeterOpen"][!ShowMeter "MeterClosed"][!SetOption "MeterLuaReturn" "Text" "[#CURRENTSECTION#]"]
Then remove "MeasureName" from "MeterShopMotion" (text is set by "SetOption" in the Match-code above).

This way you can get rid of "[MeasureLuaScript]".

What is wrong with your code?
I'm not completely sure but in your lua-code you doubled the Show/Hide for your meters and as a second possible error you code SKIN:Bang('!ShowMeter "MeterOpen"') instead of SKIN:Bang('!ShowMeter', 'MeterOpen') (the same for the "Hide"-Bang(s)).
User avatar
jsmorley
Developer
Posts: 19269
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Conditional (if/else) may not be working

jsmorley » May 10th, 2016, 11:26 am

ikarus1969 is right that this is probably not a good candidate for Lua. Not that you can't do it in Lua, but you have logic that can be done in a simple IfMatch and some actions right in the skin.

Having said that, I would make one other observation. While there are times that hiding one meter and showing another is the right way to deal with a result, !SetOption can very often accomplish what you want with a bit less brute-force.

For instance:

[SomeMeasure]
...
IfMatch="switch on"
IfMatchAction=[!HideMeter "SwitchOff"][!ShowMeter "SwtichOn"]
IfNotMatchAction=[!HideMeter "SwitchOn"][!ShowMeter "SwtichOff"]

[SwitchOn]
Meter=Image
ImageName=SwitchOn.png

[SwitchOff]
Meter=Image
ImageName=SwitchOff.png

can be done a bit more efficiently (code-wise) with:

[SomeMeasure]
...
IfMatch="switch on"
IfMatchAction=[!SetOption SwitchState ImaegName "SwitchOn.png"]
IfNotMatchAction=[!SetOption SwitchState ImaegName "SwitchOff.png"]

[SwitchState]
Meter=Image
ImageName=SwitchOff.png

Hey, you save a whole meter... Since Bytes are a limited resource, and the world is rapidly running out of them, every Byte you save is one more that will be available to your children.
User avatar
jsmorley
Developer
Posts: 19269
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Conditional (if/else) may not be working

jsmorley » May 10th, 2016, 11:56 am

If you are going to do this in Lua, and you might well if there are other "value added" things you want to do in the Lua, it would look like this:

Code: Select all

function initialize()
   
	mtMeasureToSetValue = SKIN:GetMeasure('MeasureShopMotion')

end

function update()

	MeasureShop = mtMeasureToSetValue:GetStringValue()
   
	if MeasureShop == 'Moving' then
		SKIN:Bang('!ShowMeter', 'MeterOpen')
		SKIN:Bang('!HideMeter', 'MeterClosed')
	else
		SKIN:Bang('!ShowMeter', 'MeterClosed')
		SKIN:Bang('!HideMeter', 'MeterOpen')
	end 
   
   return MeasureShop
   
end
There is really no need to ever get a "handle" to meters with

mtOpenMeter = SKIN:GetMeter('MeterOpen')

and use meter-specific functions like

mtOpenMeter:Show()

Those are really deprecated by Rainmeter bangs like !ShowMeter (in this case) or !SetOption (in most cases).

One other tip I would give is that the old "bang" method in Lua:

Old and busted: SKIN:Bang('!SetOption SomeMeter FontColor 255,255,255,255')

Where you create a single string that contains the entire bang and parameters,

has been replaced by one where the bang name and parameters are just separate components of the SKIN:Bang function:

New hotness: SKIN:Bang('!SetOption', 'SomeMeter', 'FontColor', '255,255,255,255')

The HUGE advantage to this is that parameters that have spaces in them don't have to have complicated quoting to ensure Rainmeter understands what is a space IN a parameter, and what is a space that SEPARATES parameters.
RKC_317
Posts: 2
Joined: May 9th, 2016, 11:14 pm

Re: Conditional (if/else) may not be working

RKC_317 » May 10th, 2016, 7:00 pm

jsmorley wrote:
Those are really deprecated by Rainmeter bangs like !ShowMeter (in this case) or !SetOption (in most cases).

One other tip I would give is that the old "bang" method in Lua:

Old and busted: SKIN:Bang('!SetOption SomeMeter FontColor 255,255,255,255')

Where you create a single string that contains the entire bang and parameters,

has been replaced by one where the bang name and parameters are just separate components of the SKIN:Bang function:

New hotness: SKIN:Bang('!SetOption', 'SomeMeter', 'FontColor', '255,255,255,255')

The HUGE advantage to this is that parameters that have spaces in them don't have to have complicated quoting to ensure Rainmeter understands what is a space IN a parameter, and what is a space that SEPARATES parameters.
Thank you so much!

This really improved the LUA code a lot so now it's running a little more smoothly than the last time.
User avatar
jsmorley
Developer
Posts: 19269
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Conditional (if/else) may not be working

jsmorley » May 10th, 2016, 7:08 pm

Glad to help.