It is currently March 29th, 2024, 3:02 pm

Some changes to functions in Lua scripts

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Some changes to functions in Lua scripts

Post by jsmorley »

There have been several nice changes to Lua that I wanted to expand on a bit.

1) The PROPERTIES table used at the top of the Lua script to retrieve "settings" from the Script measure in the skin has been eliminated. The new SELF: prefix, which will allow you to access the calling script measure without needing any SKIN:GetMeasure() function allowed us to improve and add to the GetOption() / GetNumberOption() functions to remove the need for the PROPERTIES table.

So in a skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1

[Variables]
VarSetting=Neither

[MeasureTime]
Measure=Time
Format=%#S

[MeasureCalc]
Measure=Calc
Formula=MeasureTime
IfAboveValue=29
IfAboveAction=!Execute [!SetOption MeasureLuaScript ActionSetting 100][!SetVariable VarSetting "Above"]
IfBelowValue=30
IfBelowAction=!Execute [!SetOption MeasureLuaScript ActionSetting ""][!SetVariable VarSetting "Below"]

[MeasureLuaScript]
Measure=Script
ScriptFile=Test.lua
NumberSetting=5
StringSetting="Rainmeter"
FormulaSetting=(#SCREENAREAWIDTH# / 2)
MeasureSetting=[MeasureTime]
VarSetting=#VarSetting#
UpdateDivider=5
DynamicVariables=1

[MeterOne]
Meter=Image
SolidColor=255,255,255,255
W=20
H=20
Then in the script:

Code: Select all

function Initialize()

   sStringSetting = SELF:GetOption('StringSetting', 'N/A')
   iNumberSetting = SELF:GetNumberOption('NumberSetting')
   
end -->Initialize

function Update()

   local iFormulaSetting = SELF:GetNumberOption('FormulaSetting')
   local sMeasureSetting = SELF:GetNumberOption('MeasureSetting')
   local sVarSetting = SELF:GetOption('VarSetting')
   local sActionSetting = SELF:GetOption('ActionSetting')   
   
   print(iNumberSetting, sStringSetting, iFormulaSetting, sMeasureSetting, sVarSetting, sActionSetting)
   
   return "Done"
   
end -->Update

Code: Select all

Debug	00:33:34.469	5    Rainmeter    840    52    Above    100
We have added an a new GetNumberOption() function that will retrieve a number from a measure setting without needing to do any tonumber(), and have added a new optional default value to both GetOption() and GetNumberOption(). GetNumberOption() will also parse and formulas and resolve the value of measures passed in the setting.

As you can see, this both eliminates the need for the PROPERTIES table, and adds some new flexibility and functionality.

2) We have added a new ParseFormula() function, which will allow a Lua script to treat a string containing a formula as a formula and return the value result. This is going to be particularly useful in sending formulas and measure values in a !CommandMeasure statement to a script.

Skin:

Code: Select all

[MeterOne]
Meter=Image
SolidColor=255,255,255,255
W=20
H=20
LeftMouseUpAction=!CommandMeasure MeasureLuaScript "MyFunc('([MeasureTime] * 1.7)')"
Script:

Code: Select all

function MyFunc(Parm)

   iCalculatedValue = SKIN:ParseFormula(Parm)
   print('Function Value: '..iCalculatedValue)

end -->MyFunc
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Some changes to functions in Lua scripts

Post by AlC »

The changes are really nice, great improvement ! :thumbup:

But I found this in the example script in the manual
Manual wrote:function Initialize()
exampleNumber = SELF:ReadNumber('ExampleNumber')
end
What is SELF:ReadNumber ? Is SELF:ReadNumber maybe wrong and it should be SELF:GetNumberOption ?
Rainmeter - You are only limited by your imagination and creativity.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Some changes to functions in Lua scripts

Post by jsmorley »

AlC wrote:The changes are really nice, great improvement ! :thumbup:

But I found this in the example script in the manual What is SELF:ReadNumber ? Is SELF:ReadNumber maybe wrong and it should be SELF:GetNumberOption ?
Ah, that was left from an earlier bit of testing we were doing. Yes, that should be GetNumberOption. Fixed now, thanks.
User avatar
AlC
Posts: 329
Joined: June 9th, 2011, 6:46 pm

Re: Some changes to functions in Lua scripts

Post by AlC »

jsmorley wrote:Ah, that was left from an earlier bit of testing we were doing. Yes, that should be GetNumberOption. Fixed now, thanks.
Okay thanks. :)
Rainmeter - You are only limited by your imagination and creativity.