It is currently March 28th, 2024, 6:46 pm

Basics of variables exchanging

Discuss the use of Lua in Script measures.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Basics of variables exchanging

Post by rbriddickk84 »

Greetings!

I try to make my mind around how the skin-ini and lua communicating.
More exactly i try to pass a dynamic variable to lua to process changes without refreshing my skin.
Whatever i tried, didn't worked. Tried to changed the value under [Variables], no changes in lua.
Tried also under the ScriptMeasure's MyOption, and tried changing that option with !SetOption Bang, but still not changed in Lua.

What i want to achieve is this:
- There is a variable in the INI file
- Get that variable's value in LUA
- Changing the value of the variable in the INI file
- Get the changed value in the LUA

Achieving this without any refreshing of the INI file. (if possible)
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Basics of variables exchanging

Post by FreeRaider »

Please, post a sample skin/code
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Basics of variables exchanging

Post by balala »

Posting a sample code, as FreeRaider asked would definitely be a good idea, however I can say some generalities:
rbriddickk84 wrote:What i want to achieve is this:
- There is a variable in the INI file
...
Eg, let's say, you have the following variable, defined in the [Variables] section of your skin:

Code: Select all

[Variables]
MyVar=24
You also have to create a Script measure, which will handle the lua code:

Code: Select all

[MeasureLuaScript]
Measure=Script
ScriptFile=#@#Change.lua
rbriddickk84 wrote:What i want to achieve is this:
...
- Get that variable's value in LUA
...
About the lua code you have to know that it has two very important functions, Initialize() and Update(). Initialize() is executed once, immediately after you're refreshing your skin, while Update() is executed on each update cycle of the skin. To get the value of the variable into the lua code, add the following command to the Update() function: MyVar = tonumber(SKIN:GetVariable('MyVar')). The SKIN:GetVariable('MyVar')) expression gets the variable, while the tonumber() function converts this gotten value, to a number.
Now you can even process this value:

Code: Select all

MyVar = MyVar + 2
(add this command after the previous one). This command will add 2 to the gotten variable.
If the variable is changing in the ini file, the lua script will get automatically the changed value (see immediately).

rbriddickk84 wrote:What i want to achieve is this:
...
- Get the changed value in the LUA
Now the last thing would be to transfere the processed variable back to the ini file. This will be done for example by the following command (add this after the previous command): SKIN:Bang('!SetVariable', 'NewVar', MyVar). See that the value of the MyVar variable will be transferred to the NewVar variable, which can be used into the ini file, eg in the following String meter:

Code: Select all

[MeterVariables]
Meter=STRING
X=0
Y=0
Padding=15,5,15,5
FontColor=220,220,220
SolidColor=0,0,0,150
FontSize=8
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=#MyVar##CRLF##NewVar#
DynamicVariables=1
Very important here is the DynamicVariables=1 option. Without this, the string meter can't properly get the variable, changed dynamically by the lua script.
rbriddickk84 wrote:What i want to achieve is this:
...
- Changing the value of the variable in the INI file
Now one more: to see how the dynamic change works, you have to add eg a LeftMouseUpAction=[!SetVariable MyVar "(#MyVar#+1)"] option to the [MeterVariables] string meter. This way you're dynamically changing the value of the variable and the processed NewVar will follow the change, through the lua script.
The whole codes:
.ini:

Code: Select all

[Rainmeter]
Update=1000

[Variables]
MyVar=24

[MeasureLuaScript]
Measure=Script
ScriptFile=#@#Change.lua

[MeterVariables]
Meter=STRING
X=0
Y=0
Padding=15,5,15,5
FontColor=220,220,220
SolidColor=0,0,0,150
FontSize=8
FontFace=Segoe UI
StringStyle=BOLD
StringAlign=LEFT
AntiAlias=1
Text=#MyVar##CRLF##NewVar#
DynamicVariables=1
LeftMouseUpAction=[!SetVariable MyVar "(#MyVar#+1)"]
Change.lua:

Code: Select all

function Initialize()
end

function Update()
	MyVar = tonumber(SKIN:GetVariable('MyVar'))
	MyVar = MyVar + 2
	SKIN:Bang('!SetVariable', 'NewVar', MyVar)
end
Place this script to the @Resources folder.

Please let me know if this answer your question.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Basics of variables exchanging

Post by rbriddickk84 »

FreeRaider wrote:Please, post a sample skin/code
Okay, i wrote one quickly here, my code is very chaotic to paste in here.

INI code:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1

[Variables]
CalYear=0

[DateYear]
Measure=Time
Format=%G
OnUpdateAction=[!SetOption "CalendScript" "MyYear" "[DateYear]"][!SetVariable CalYear "[DateYear]"]

[CalendScript]
Measure=Script
ScriptFile="#CURRENTPATH#@Resources\Scripts\calendar.lua"
MyYear=2000
DynamicVariables=1
UpdateDivider=-1

LUA code:

Code: Select all

function Initialize()
idYear = SELF:GetOption('MyYear', 'n/a')
idSecYear = SKIN:GetVariable('CalYear', 'n/a')

end 
function Update()

print(idYear)
print(idSecYear)

end -- function Update
The code correctly gets the option, but it cannot be changed. Also gets the variable's value, but if that is changed in INI, it doesn't change in LUA.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Basics of variables exchanging

Post by rbriddickk84 »

Thank you balala, i saw your post after i posted mine.
I posted an example code ;) Sorry about it, i wasn't at my computer for awhile.
I just wanted to get the LUA script the changed value without refreshing the skin ini. :)
To be honest, what you wrote that all i know already. :) But thank you for your help ;)
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Basics of variables exchanging

Post by balala »

rbriddickk84 wrote:I just wanted to get the LUA script the changed value without refreshing the skin ini. :)
You don't even have to. See below.
rbriddickk84 wrote:To be honest, what you wrote that all i know already. :) But thank you for your help ;)
Please forgive me, but it seems you don't! I say that because you've placed the GetOption and GetVariable into the Initialize() function, which, as I wrote above, is executed just once, when you're refreshing your skin. That's why they can't get the newly set values. Just move them into the Update() function. This is the main problem.

I also would have something to say about how you're returning the processed values from the lua script to the skin: I usually always try to avoid using the print function. In my opinion a much - much better alternative would be to transfere these values back through some SKIN:Bang('!SetVariable', 'VariableName', Value), or SKIN:Bang('!SetOption', 'SectionName', 'OptionName', Value) commands. Will avoid a lot of headaches.
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Basics of variables exchanging

Post by rbriddickk84 »

It's okay, but i still say, i do know, but only that one thing i missed! :) To be precise. :D
But anyway, i see now where i was mistaken, and i should have figured it out by myself, because that is a minor thing, so i was incredibly blind! xD

Thank you very much for pointing that out, and for the help! ;)

:thumbup:
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Basics of variables exchanging

Post by balala »

rbriddickk84 wrote:It's okay, but i still say, i do know, but only that one thing i missed! :) To be precise. :D
But anyway, i see now where i was mistaken, and i should have figured it out by myself, because that is a minor thing, so i was incredibly blind! xD

Thank you very much for pointing that out, and for the help! ;)

:thumbup:
I hope I didn't offend you, because I didn't want to. Sorry if however I did. Please forgive me...
But, I still have a two comments:
User avatar
rbriddickk84
Rainmeter Sage
Posts: 276
Joined: February 17th, 2014, 12:39 pm
Location: Hungary

Re: Basics of variables exchanging

Post by rbriddickk84 »

balala wrote:I hope I didn't offend you, because I didn't want to. Sorry if however I did. Please forgive me...
Hey, don't worry, it's okay :D I wasn't that thorough while reading your answer, and slipped over details, so my bad!
balala wrote:But, I still have a two comments:
I use the @Resources folder like that, because my skin is currently part of a parent skin group. So this is in the AstroTech4 folder, where there is also a @Resources folder. But when i finished with the project, i will change the code back to #@#.
Because when i am finished, i will make this skin a standalone skin, outside of AstroTech4 folder. :)

And i removed the quotations, just to be sure :) :great:

Again, thanks for your help mate!