It is currently July 4th, 2022, 5:23 am

lua button variables

Discuss the use of Lua in Script measures.
acidchu
Posts: 1
Joined: March 6th, 2022, 2:13 am

lua button variables

Post by acidchu »

Hey im rather new to lua so bare with me here im sure theres a better way to do this but i have a python script that is able to write its variables to a dif text file for each i want to be able to use rainmeter + lua to be able to change them on a button press but if the code changes it needs to update the skin ie the image for the button aswell. my solution works its just a bit clunky and unreliable any suggestions will be appreciated.

lua

Code: Select all

function Initialize()

    file = SELF:GetOption('File_play')

    play_file_w = io.open(file, 'w')
    play_file_w:write(0)
    io.close(play_file_w)

    last_play = 0
end


function Update()
    play_file = io.open(file)
    play_read = play_file:read("*all")
    play_read = string.gsub(play_read, "\t", "     ")
    play_file:close()

    SKIN:Bang('!SetVariable', 'ERROR_MSG', play_read)
    SKIN:Bang('!SetVariable', 'last_play', last_play)
    play_contents = SKIN:GetVariable("play_var")

    if tonumber(play_read) ~= tonumber(last_play) then
        SKIN:Bang('!ToggleMeter', "Background.Play")
        SKIN:Bang('!ToggleMeter', "Image.Play")
        SKIN:Bang('!ToggleMeter', "Background.Pause")
        SKIN:Bang('!ToggleMeter', "Image.Pause")
        SKIN:Bang('!SetVariable', 'play_var', play_read)
        last_play = tostring(play_read)
        SKIN:Bang('!SetVariable', 'ERROR_MSG1', last_play)
    end

    
    

    if tonumber(play_contents) ~= tonumber(last_play) then
        SKIN:Bang('!ToggleMeter', "Background.Play")
        SKIN:Bang('!ToggleMeter', "Image.Play")
        SKIN:Bang('!ToggleMeter', "Background.Pause")
        SKIN:Bang('!ToggleMeter', "Image.Pause")
        last_play = tostring(play_contents)
        SKIN:Bang('!SetVariable', 'last_play', last_play)
        

        
        play_file_w = io.open(file, 'w')
        play_file_w:write(play_contents)
        io.close(play_file_w)
    end
end
rainmeter

Code: Select all

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

[Variables]
play_var=0
skip_var=0
back_var=0
shuffle_var=0
repeat_var=0
ERROR_MSG="A"
ERROR_MSG1="A"
last_play=0

[MeasureName]
Measure=Script
ScriptFile=test5.lua
File_play =C:\Users\----\Documents\Rainmeter\Skins\acidchu_v2\test\play_text.txt
DynamicVariables=1


;;===========================================

[Background.Play]
 Meter=Shape
 X=50r
 Y=r
 Hidden=1
 Shape=Rectangle 0,0,26,26 | StrokeWidth 0 | Fill Color 255,255,255,1
 LeftMouseUpAction=[!SetVariable "play_var" "0"][!Update]


[Image.Play]
 Meter=Image
 ImageName=Play.png
 X=r
 Y=r
 W=26
 Hidden=1
 LeftMouseUpAction=[!SetVariable "play_var" "0"][!Update]


[Background.Pause]
 Meter=Shape
 X=r
 Y=r

 LeftMouseUpAction=[!SetVariable "play_var" "1"][!Update]

[Image.Pause]
 Meter=Image
 ImageName=Pause.png
 X=r
 Y=r
 W=26
  LeftMouseUpAction=[!SetVariable "play_var" "1"][!Update]
User avatar
Yincognito
Rainmeter Sage
Posts: 4724
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: lua button variables

Post by Yincognito »

Can't test the code from my phone ATM, but it's not quite clear what exactly are you trying to do. Do you want to change or toggle the hidden state of meters from Lua? Do you want to change or toggle the value of the ImageName options to different paths and files?

Anyway, personally, from the top of my head, I believe that you can do without so many SKIN:... controlling from Lua (I don't like those as they introduce dependencies of the code from a specific skin, meaning the Lua part will have 0 portability and reusability for a similar skin with a relatively similar yet different code, but that's just me) and instead set a few variables controlling that, which you can then use from the Rainmeter code via [Script:SomeLuaVariable] inline syntax.

As for the rest, more details are needed on precisely what you're trying to accomplish and what those variables actually mean. The load and save processes are good, no potential issues there, I think.

And yeah, in some cases you'd need a skin refresh to do things, but I'm not convinced this would be the case here, from the limited info that you provided.

As a side note, I recently completed a rather complete CSV like file "editor" with Lua and Rainmeter parts, for a different topic, but which is largely reusable. Not sure if it's similar to what you have in mind, but I'm just saying, in case it is... :confused:
User avatar
death.crafter
Rainmeter Sage
Posts: 1367
Joined: April 24th, 2021, 8:13 pm

Re: lua button variables

Post by death.crafter »

acidchu wrote: March 7th, 2022, 9:21 am Hey im rather new to lua so bare with me here im sure theres a better way to do this but i have a python script that is able to write its variables to a dif text file for each i want to be able to use rainmeter + lua to be able to change them on a button press but if the code changes it needs to update the skin ie the image for the button aswell. my solution works its just a bit clunky and unreliable any suggestions will be appreciated.
If you could provide the full context of what you want to do, it would be better. Your code seems to say little about what you want to achieve.
from the Realm of Death