It is currently March 28th, 2024, 9:58 pm

LUA Variables & "nil" error.

Discuss the use of Lua in Script measures.
Thompson820
Posts: 13
Joined: October 20th, 2011, 10:57 am

LUA Variables & "nil" error.

Post by Thompson820 »

I've been working on updating "Th3Laugh1ngMan"'s Persona 4 HUD skin (the skin's .ini file specifically allows editing and redistribution through Creative Commons if original author(s) are credited) with more features and cleaner code. I initially hoped i'd be able to dodge using LUA but soon found that the things I wanted to do were impossible without LUA, so i've made the jump and have made major progress, however I am now stuck on the final piece of code.

I'll put the files in code form below and the entire skin in download form here.

Code: Select all

;Updated by Thompson820. I've done a major rehaul of the way this skin works. This skin will now display Midnight and Noon as time periods.
;It also has a different set of icons for nighttime weather, at the moment the only icons that are different at night are for 'sunny',
;'partlycloudy' and 'mostlyclear' which all were originally the sun and are now the moon at night.
;I don't think the other icons need changing, since they're all excellent and represent their respective weather conditions well.
;This skin now uses LUA files as well as this .ini file, the LUA makes it easier to choose the correct time period.
;---------------------------------------
;	Concept, design and original code. = Th3Laugh1ngMan  http://th3laugh1ngman.deviantart.com/
;---------------------------------------
;   WEATHER UNDERGROUND Widget & Code
;	http://www.wunderground.com/
;	Original Author = maminscris  http://maminscris.deviantart.com/
;	Original CoAuthor = Yelleke   http://yelleke.deviantart.com/
;---------------------------------------
;	TIME CODE
;	Originally from anelectricQlock by anelectricmind, modified by Th3Laugh1ngMan.
;---------------------------------------
[Metadata]
Name= Persona 4 HUD v2
Config=
Description=An updated version of Persona 4 HUD by Th3Laugh1ngMan.
Instructions=REQUIRES that the standard theme still be installed - it accesses a .dll there. Make sure to snap it to the top right of your screen for optimal awesomeness. Change the Variable "Location" to your local weather, from http://www.wunderground.com/.
Version=2.0
Tags=Persona4, Persona 4
License=Creative Commons etc. Feel free to tear things apart, but remember to credit me, Th3Laugh1ngMan for the original idea and design and the people whose code remains in here.
Variant=
Preview=
[Variables]
Location=Castlemaine,Australia
NowTime=Test
PreTime=Test
WeatherTime=Day
[Main]
Meter=IMAGE
ImageName=PersonaBG.png
W=392
H=200
[TIME]
Measure=Time
Format=%#H:%M
[DATE]
Measure=Time
Format=%m/%d   %A
Substitute="Monday":"Mon","Tuesday":"Tue","Wednesday":"Wed","Thursday":"Thu","Friday":"Fri","Saturday":"Sat","Sunday":"Sun"
[(time)]
MeasureName=TIME
Meter=String
X=110
Y=29
FontColor=ffffff
FontFace=Arial
FontSize=22
AntiAlias=1
StringAlign=RIGHT
[(date)]
MeasureName=DATE
Meter=String
X=130
Y=29
FontColor=ffffff
FontFace=Arial
FontSize=22
AntiAlias=1
StringAlign=LEFT
Style=BOLD
[measureHour]
Measure=Time
Format=%H
DynamicVariables=1
[Midnight]
Meter=IMAGE
ImageName=TimePeriods\Midnight.png
y=5
Hidden=1
[EarlyMorning]
Meter=IMAGE
ImageName=TimePeriods\EarlyMorning.png
y=5
Hidden=1
[Morning]
Meter=IMAGE
ImageName=TimePeriods\Morning.png
y=5
Hidden=1
[Noon]
Meter=IMAGE
ImageName=TimePeriods\Noon.png
y=5
Hidden=1
[Afternoon]
Meter=IMAGE
ImageName=TimePeriods\Afternoon.png
y=5
Hidden=1
[Evening]
Meter=IMAGE
ImageName=TimePeriods\Evening.png
y=5
Hidden=1
[Night]
Meter=IMAGE
ImageName=TimePeriods\Night.png
y=5
Hidden=1
[Test]
Meter=IMAGE
ImageName=TimePeriods\Test.png
y=5
Hidden=1
;--------------------------------
;This is where I activate the lua file. I don't have to send any values, so I only need this code.
;Note that you have to define a 'TableName', this can be anything you like.
;--------------------------------
[ToLua]
Measure=Script
ScriptFile=#CURRENTPATH#Time.lua
TableName=Time
UpdateDivider=15
DynamicVariables=1
;--------------------------------
;This measure will only run once (it disables itself at the end of it's actions) when the first new variable for NowTime is returned from LUA and sets the PreTime to be the same as NowTime.
;--------------------------------
[RunOnce]
Measure=calc
Formula=(1+1=2) ? 1:0
IfEqualValue=1
IfEqualAction=!Execute [!SetVariable PreTime #NowTime#] [!DisableMeasure RunOnce]
DynamicVariables=1
;--------------------------------
;This measure send off for LUA to compare NowTime and PreTime, when one is inequal to the other it hides the meter related to PreTime and then forces it to become equal to NowTime.
;This makes sure that when a new time period image is to be displayed the old one is always wiped away. No nasty overlapping!
;--------------------------------
[ToLua2]
Measure=Script
ScriptFile=#CURRENTPATH#Compare.lua
TableName=Compare
UpdateDivider=15
DynamicVariables=1
[MeasureWunder]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=1200
Url=http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#Location#
RegExp="(?siU)<display_location>.*<city>(.*)</city>.*</display_location>.*<observation_time>Last Updated on (.*), (.*) (.*)</observation_time>.*<weather>(.*)</weather>.*<temp_c>(.*)</temp_c>.*<relative_humidity>(.*)</relative_humidity>.*<wind_dir>(.*)</wind_dir>.*<wind_mph>(.*)</wind_mph>.*<pressure_mb>(.*)</pressure_mb>.*<dewpoint_c>(.*)</dewpoint_c>.*<icon>(.*)</icon>"
FinishAction=!EnableMeasure Display
;--------------------------------
;The LUA file sends the new value for #NowTime# back and this Measure (with an always true formula) will always show the new variable's related Meter.
;--------------------------------
[Display]
Measure=calc
Formula=(1+1=2) ? 1:0
IfEqualValue=1
IfEqualAction=!ShowMeter #NowTime#
Disabled=1
DynamicVariables=1
[MeasureCurIcon]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[MeasureWunder]
StringIndex=12
;--------------------------------
;This code is almost identical to the original, the only difference is adding #WeatherTime# to the file path. I had to make the default value for #WeatherTime# day so that
;the skin will always at least show the day icons, but when the LUA sends back the new value for the variable it will show the correct icons.
;--------------------------------
[MeterCurIcon]
MeasureName=MeasureCurIcon
Meter=IMAGE
X=290
Y=105
H=78
W=78
Path=WeatherIcons\#WeatherTime#
LeftMouseUpAction=!Execute [!RainmeterRefresh]
DynamicVariables=1

Code: Select all

PROPERTIES =
{

}

function Initialize()

	H = SKIN:GetMeasure("measureHour")
	
end

function Update()
	
	HR = H:GetValue()
	
	if HR == 00 then
		SKIN:Bang("!RainmeterSetVariable NowTime Midnight")
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("0 fired")
	
	elseif HR == 01 then
		SKIN:Bang("!RainmeterSetVariable NowTime EarlyMorning")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("1 fired")
		
	elseif HR == 02 then
		SKIN:Bang("!RainmeterSetVariable NowTime EarlyMorning")
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("2 fired")
		
	elseif HR == 03 then
		SKIN:Bang("!RainmeterSetVariable NowTime EarlyMorning")
	SKIN:Bang("!RainmeterSetVariable WeatherTime Night")		
		print("3 fired")
		
	elseif HR == 04 then
		SKIN:Bang("!RainmeterSetVariable NowTime EarlyMorning")
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("4 fired")
		
	elseif HR == 05 then
		SKIN:Bang("!RainmeterSetVariable NowTime EarlyMorning")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("5 fired")
		
	elseif HR == 06 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("6 fired")
		
	elseif HR == 07 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")
	SKIN:Bang("!RainmeterSetVariable WeatherTime Day")		
		print("7 fired")
		
	elseif HR == 08 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("8 fired")
		
	elseif HR == 09 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("9 fired")
		
	elseif HR == 10 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")
	SKIN:Bang("!RainmeterSetVariable WeatherTime Day")		
		print("10 fired")
		
	elseif HR == 11 then
		SKIN:Bang("!RainmeterSetVariable NowTime Morning")
	SKIN:Bang("!RainmeterSetVariable WeatherTime Day")		
		print("11 fired")
		
	elseif HR == 12 then
		SKIN:Bang("!RainmeterSetVariable NowTime Noon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("12 fired")
		
	elseif HR == 13 then
		SKIN:Bang("!RainmeterSetVariable NowTime Afternoon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("13 fired")
		
	elseif HR == 14 then
		SKIN:Bang("!RainmeterSetVariable NowTime Afternoon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("14 fired")
		
	elseif HR == 15 then
		SKIN:Bang("!RainmeterSetVariable NowTime Afternoon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("15 fired")
		
	elseif HR == 16 then
		SKIN:Bang("!RainmeterSetVariable NowTime Afternoon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("16 fired")
		
	elseif HR == 17 then
		SKIN:Bang("!RainmeterSetVariable NowTime Afternoon")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("17 fired")
		
	elseif HR == 18 then
		SKIN:Bang("!RainmeterSetVariable NowTime Evening")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Day")
		print("18 fired")
		
	elseif HR == 19 then
		SKIN:Bang("!RainmeterSetVariable NowTime Evening")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("19 fired")
		
	elseif HR == 20 then
		SKIN:Bang("!RainmeterSetVariable NowTime Night")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("20 fired")
		
	elseif HR == 21 then
		SKIN:Bang("!RainmeterSetVariable NowTime Night")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("21 fired")
		
	elseif HR == 22 then
		SKIN:Bang("!RainmeterSetVariable NowTime Night")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("22 fired")
		
	elseif HR == 23 then
		SKIN:Bang("!RainmeterSetVariable NowTime Night")	
		SKIN:Bang("!RainmeterSetVariable WeatherTime Night")
		print("23 fired")
		
	end
	
NewReturnValue = 99
return NewReturnValue
end

print("Timed."..os.date())

Code: Select all

PROPERTIES =
{

}

function Initialize()
	
	
	
end

function Update()
	
	NowTime = SKIN:ReplaceVariables("#NowTime#")
	PreTime = SKIN:ReplaceVariables("#PreTime#")
	
	if NowTime ~= PreTime then
		SKIN:Bang("!RainmeterHideMeter #PreTime#")
		SKIN:Bang("!RainmeterSetVariable PreTime #NowTime#")
		print(Changed)
		
	else
		print(Not changed)
		
	end
	
NewReturnValue = 99
return NewReturnValue
end

print("Compared."..os.date())
As you can probably tell, this is my first attempt at Rainmeter or really any coding. Everything else is miraculously working, however it's the final LUA file "Compare.lua" that is giving me grief at the moment. I'm certain it's a stupid syntax error on my part, but it's putting out "nil" errors in the Rainmeter log.

I've spent a lot of time looking at similar threads on this forum, the relevant support articles and the LUA guides but i'm not seeing the solution, no matter how many different ways I try to recode it. It would be much appreciated if someone could point me in the right direction.

Thank you for your time.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: LUA Variables & "nil" error.

Post by jsmorley »

The only real problem I see is with this bit:
if NowTime ~= PreTime then
SKIN:Bang("!RainmeterHideMeter #PreTime#")
SKIN:Bang("!RainmeterSetVariable PreTime #NowTime#")
print(Changed)

else
print(Not changed)

end
You need to put quotes around text strings and using:

if NowTime ~= PreTime then
SKIN:Bang("!RainmeterHideMeter #PreTime#")
SKIN:Bang("!RainmeterSetVariable PreTime #NowTime#")
print("Changed")

else
print("Not changed")

end

Seems to fix any errors in the log for me.
Thompson820
Posts: 13
Joined: October 20th, 2011, 10:57 am

Re: LUA Variables & "nil" error.

Post by Thompson820 »

Oh wow, I can't believe it was something so basic! Thank you for the response!
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: LUA Variables & "nil" error.

Post by jsmorley »

Glad to help.