It is currently September 20th, 2020, 8:50 pm

How to define a LUA variable from rainmeter file

Discuss the use of Lua in Script measures.
H3rcu135
Posts: 5
Joined: September 6th, 2015, 2:34 pm

How to define a LUA variable from rainmeter file

Post by H3rcu135 »

I have my lua script

Code: Select all

function Update(MyVariable)
  local v = tonumber(MyVariable)
  local r = "ss"

  if(v == 1) then
    r = "foo"
  elseif(v==2) then
    r = "bar"
  else
   r = "apple"
  end

  return tostring(r)
end
and my measure

Code: Select all

[Measure1]
Measure=Script
ScriptFile=Path/To/Script.lua
MyVariable=1
and despite setting MyVariable to 1 it returns "apple" what am I doing wrong?
User avatar
jsmorley
Developer
Posts: 21372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to define a LUA variable from rainmeter file

Post by jsmorley »

Code: Select all

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

[Measure1]
Measure=Script
ScriptFile=Test.Lua
MyVariable=1

[Meter1]
Meter=String
MeasureName=Measure1
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1

Code: Select all

function Update()

	local v = SELF:GetNumberOption('MyVariable')
	local r = "ss"

	if v == 1 then
		r = "foo"
	elseif v == 2 then
		r = "bar"
	else
		r = "apple"
	end

	return r

end
1.jpg
http://docs.rainmeter.net/manual-beta/lua-scripting#SelfObject

http://docs.rainmeter.net/manual-beta/lua-scripting#GetNumberOption
http://docs.rainmeter.net/manual-beta/lua-scripting#GetOption
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 21372
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to define a LUA variable from rainmeter file

Post by jsmorley »

Some illumination on the previous post.

There are no parameters allowed on the Initialize() or Update() functions in the Rainmeter integration with Lua. Initialize() is just executed automatically, with no parameters, when a Script measure is first set up on the load / refresh of the skin. Update() is just executed automatically, with no parameters, on each update of the Script measure.

Just defining a measure option like that MyVariable=1 on the Script measure does not in and of itself make that value a variable, or in any other way visible to the Lua.

Options on, and values for, measures can be retrieved by a two step process:

1) Create a "measure object" linking the measure to Lua. This is done with the SKIN:GetMeasure() command.

myMeasure = SKIN:GetMeasure('MeasureName')

That will create a measure object called "myMeasure", that is linked to the measure "MeasureName" in the skin. Think of it as a "pointer" to the measure in the skin.

This will normally be done in the Initialize() function of the Lua script file, since it only needs to be done once when the skin is loaded or refreshed, and not on every update of the skin or Script measure.

2) Get the value you want from the measure using that measure object.

This is done with various Measure Object commands. To get the value of an option on a measure you might use:

Number Value:
myMeasureValue = myMeasure:GetOption('MyOption')

String Value:
myMeasureValue = myMeasure:GetStringOption('MyOption')

That will return the current value of the MyOption option on the measure you created the measure object link to with the myMeasure object.


The SELF: object is a special kind of measure object, that is automatically linked to the Script measure that called the Lua script in the skin . This behaves exactly the same as the SKIN: object, with the exception that you don't first have to create the link to the desired measure, as in step 1) above. That is already done.

So to get the current value of the MyOption option on the Script measure calling the Lua, you could use:

Number Value:
myMeasureValue = SELF:GetOption('MyOption')

String Value:
myMeasureValue = SELF:GetStringOption('MyOption')

The advantage to using SELF to get Options from the Script measure are two:

First, you don't need the specific GetMeasure() command in the Initialize() function. The SELF: object is automatically created, and linked to the Script measure in the skin calling the Lua function.

Second, you could use the same code with a single option defined differently when called by multiple Script measures in your skin. Since SELF: is automatically linked to the Script measure calling the Lua function, the GetOption() GetStringOption() commands will automatically use the value of the option from the calling Script measure.