It is currently March 28th, 2024, 7:14 pm

Use of Dynamic Variables with LUA Scripts

Discuss the use of Lua in Script measures.
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Use of Dynamic Variables with LUA Scripts

Post by Mordasius »

Am I right in thinking that, like with plugins, you can't use Dynamic Variables in a LUA Script? Well you can use them but they will stay at the same value you assigned to them in the [Variables] section.

Seems to me that even if you BANG them in the LUA Script with stuff like SKIN:Bang("!SetVariable MyVariable \""..WhatIreallyWant.."\"") the change will show up in the Rainmeter Meters but can not be passed back to a LUA Script in [Measure=Script] with something like My_LUA_Variable=#WhatIreallyWant#
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Use of Dynamic Variables with LUA Scripts

Post by poiru »

Custom Setting= keys do not support dynamic variables in the SCRIPT measure. This will likely be fixed for the next beta release.

To get around this, you could use something like the following in your Update() function:

Code: Select all

SKIN:GetVariable("WhatIreallyWant")
(Note that ScriptFile= does support dynamic variables)
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Use of Dynamic Variables with LUA Scripts

Post by jsmorley »

Mordasius wrote:Am I right in thinking that, like with plugins, you can't use Dynamic Variables in a LUA Script? Well you can use them but they will stay at the same value you assigned to them in the [Variables] section.

Seems to me that even if you BANG them in the LUA Script with stuff like SKIN:Bang("!SetVariable MyVariable \""..WhatIreallyWant.."\"") the change will show up in the Rainmeter Meters but can not be passed back to a LUA Script in [Measure=Script] with something like My_LUA_Variable=#WhatIreallyWant#

You are correct that DynamicVariables can't be used for the optional Setting= stuff on a Lua script. However, there is an easy solution.

Just set the thing you want to pass as a "setting" as a [Variable] instead, and use GetVariable() in the Update() function of the Lua script to get the current value of that variable. Very dynamic indeed...
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Use of Dynamic Variables with LUA Scripts

Post by Mordasius »

poiru wrote: To get around this, you could use something like the following in your Update() function: SKIN:GetVariable("WhatIreallyWant")
and
jsmorley wrote:.... there is an easy solution. Just set the thing you want to pass as a "setting" as a [Variable] instead, and use GetVariable() in the Initialize() or Update() function of the Lua script to get the current value of that variable. Very dynamic indeed...
I was so concerned with Setting Variables that I forgot you could also Get them from the LUA script.

As you say, very easy and very dynamic.

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

Re: Use of Dynamic Variables with LUA Scripts

Post by jsmorley »

You are welcome.

It is one of the things that makes the Lua/Rainmeter interaction a pleasure for me. In a sense, variables are ALWAYS dynamic in a Lua script, since at any time you can use GetVariable() to find out what their current values are and use as you need.
sobaka
Posts: 9
Joined: September 30th, 2011, 6:56 am

Re: Use of Dynamic Variables with LUA Scripts

Post by sobaka »

If a script (eg 123.lua) change the variable in the file "var.inc"
I can get the value of this variable in another script?
write to file only option ?

PS: Sorry google translate ...
User avatar
Mordasius
Posts: 1167
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Use of Dynamic Variables with LUA Scripts

Post by Mordasius »

sobaka wrote:If a script (eg 123.lua) change the variable in the file "var.inc"
I can get the value of this variable in another script? write to file only option ?
Yes I think you can. If your var.inc is in a folder called 'Settings', then you can use something like the following in your 123.lua script:

Code: Select all

sIncFilePath = SKIN:GetVariable('ROOTCONFIGPATH')
  SKIN:Bang('!WriteKeyValue Variables WinPosX 500 "'..sIncFilePath..'Settings\\var.inc"')
The variable 'WinPosX' will have been set to 500 and can be used by any other skin (with or without a Lua script) as soon as that skin has been refreshed.

If you are looking for something that doesn't rely on !WriteKeyValue then we'll have to wait for the experts to chime in with ideas.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Use of Dynamic Variables with LUA Scripts

Post by jsmorley »

While you can read and write files directly in Lua, in the instance where you are setting a value in a ".ini" formatted file, like a variable in a .inc file. !WriteKeyValue is far and away the easiest way to do it, and you are going to have to refresh all skins using the .inc file (and thus the variable) in any case.
sobaka
Posts: 9
Joined: September 30th, 2011, 6:56 am

Re: Use of Dynamic Variables with LUA Scripts

Post by sobaka »

see example in .zip file

I do so:
1: create a file temp.txt in a folder sk2
2: add in the script sk1.lua
new = "file path"
r = io.open(SKIN:GetVariable("path").."temp.txt","w")
if not r then
print("not file")
end
r:write(""..new.."")
r:close()
3:add in the script sk2.lua
nr = io.open(SKIN:GetVariable("currentpath").."temp.txt","r")
rr = nr:read()
SKIN:Bang("!setvariable newvar "..rr.."")
nr:close()
4:changing the line in sk2.ini :
leftmouseupaction = !execute ["#program#""#var#"][!update]
to
leftmouseupaction = !execute ["#program#""#newvar#"][!update]


temp.txt used for transmission lines ...
This can simplify ?
would be very grateful for any response, thanks :) ...

PS:I make the menu "open with" for this skin http://sobaka-bim.deviantart.com/#/d4dsqbh
You do not have the required permissions to view the files attached to this post.