It is currently May 23rd, 2022, 4:37 am

Inline Lua Section Variables

Changes made during the Rainmeter 4.1 beta cycle.
User avatar
jsmorley
Developer
Posts: 22519
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Inline Lua Section Variables

Post by jsmorley »

Ok, so this is a big deal...

Lua scripting may now be used "inline" in Rainmeter.

It can either be a return of a single value from an Function() in your Lua, or a return of any variable that is set in your Lua.

This is done by using a call to a Lua script as a [SectionVariable], referencing the Script measure containing the ScriptFile option for the .lua file. The value returned by Lua will "replace" the inline Lua section variable where it is used.

These may be used anywhere a [SectionVariable] is allowed. This can be in almost any options in measures or meters, and any bangs in an action.

There is a fair amount to this, and I won't try to go into all the details here. It's all explained, and examples provided, at:

https://docs.rainmeter.net/manual-beta/lua-scripting/inline-lua/

Here is one example:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

; Another demonstration of inline Lua. This one simply passes the number
; in [MeasureBigRandom] to a FormatCommas() function in the Lua,
; and the function returns the number formatted with commas, replacing
; the inline Lua section variable with the value in the Text option.

[MeasureBigRandom]
Measure=Calc
Formula=Random
LowBound=1000
HighBound=99999999
UpdateRandom=1

[MeasureScript]
Measure=Script
ScriptFile=AddCommas.lua
Disabled=1

[MeterAddCommas]
Meter=String
FontSize=13
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Value is: [&MeasureScript:FormatCommas([&MeasureBigRandom])]
DynamicVariables=1
Lua:

Code: Select all

function FormatCommas(numVar)

	local prefix, number, postfix = string.match(numVar, '^([^%d]*%d)(%d*)(.-)$')
	return prefix..(number:reverse():gsub('(%d%d%d)', '%1,'):reverse())..postfix
   
end
1.png
And another one:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

; Demonstrates combining executing some Lua based on an "action"
; in the skin, with some inline Lua section variables to get 
; different parts of the result.

[MeasureScript]
Measure=Script
ScriptFile=FlipCoin.lua
; There is no Update() function in the Lua, and we don't need
; one, so there is no reason to enable this measure. It's a 
; "host" for the .lua file we use with !CommandMeasure and
; inline Lua later on.
Disabled=1

[MeterFlip]
Meter=String
FontSize=13
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Flip Coin
; We don't want the coin to flip on every update of the skin, or the meters with 
; the inline Lua section variables, we want to flip the coin on demand. So we
; Use a !CommandMeasure bang with a LeftMouseUpAction to execute the 
; FlipCoin() function in the Lua, which will get a random number between 0 and 1, 
; and set the value of the coin to "heads" or "tails".
LeftMouseUpAction=[!CommandMeasure MeasureScript "FlipCoin()"][!UpdateMeterGroup Coins][!Redraw]

[MeterCoin]
Meter=String
Group=Coins
X=15R
FontSize=20
FontWeight=700
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
; Now we use an inline Lua section variable to ask for the current value,
; 'Heads' or 'Tails' of the coin, by asking for the current value of the 
; variable flipText in the Lua.

; This will get that value, which was created and changed by FlipCoin() and
; is global, and replace the inline Lua section variable in the Text
; option of the meter.
Text=It's [&MeasureScript:flipText]
DynamicVariables=1

[MeterTotal]
Meter=String
Group=Coins
X=0
Y=10R
FontSize=17
FontWeight=400
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
; When we flip the coin with our !CommandMeasure action, the Lua keeps track
; of how many times the function has been called. We can then ask the Lua for the
; current value of the variable totalFlips, and that value will replace the
; inline Lua section variable in the Text option.
Text=Total Flips: [&MeasureScript:totalFlips]
DynamicVariables=1

[MeterCount]
Meter=String
Group=Coins
X=0
Y=10R
FontSize=17
FontWeight=400
FontColor=255,255,255,255
SolidColor=0,0,0,1
AntiAlias=1
; When we flip the coin with our !CommandMeasure action, the Lua also keeps track
; of how many times it was 0 or 1. We can then ask the Lua for the current value
; of either headsCount or tailsCount to return either the number of 'Heads' or 'Tails',
; and those values will replace the inline Lua section variables in the Text option.
Text=Heads: [&MeasureScript:headsCount]  Tails: [&MeasureScript:tailsCount]
DynamicVariables=1
Lua:

Code: Select all

math.randomseed(tonumber(tostring(os.time()):reverse():sub(1,6)))

-- Since the skin will be asking for these variables before the FlipCoin() function is ever
-- called, we need to set these to some initial value in the global scope, so they are not
-- initially nil, which would raise an error.

flipText = 'None'
headsCount = 0
tailsCount = 0   
totalFlips = 0
   
function FlipCoin()
   
   flipNumber = math.random(0, 1)
   
   -- When we "flip the coin", the variables we want are set by the function, and
   -- since they are not defined as local, they will be available in the global scope
   -- when we ask for them with the inline Lua in the skin.
   
   if flipNumber == 0 then
      flipText = 'Heads'
      headsCount = headsCount + 1
   else
      flipText = 'Tails'
      tailsCount = tailsCount + 1
   end
   
   totalFlips = headsCount + tailsCount

end
GIF.gif
You do not have the required permissions to view the files attached to this post.
User avatar
raiguard
Posts: 659
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Inline Lua Section Variables

Post by raiguard »

...woah.

The ideas are already flooding in! This is amazing!
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017