It is currently March 28th, 2024, 8:33 pm

Using variables in a loop.

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post by kyriakos876 »

balala wrote:Try this: SKIN:Bang(string.format('!WriteKeyValue Variables B%s "1" [color=#FF0000]"[/color]#@#FileToEdit.txt[color=#FF0000]"[/color]', LastAdded))
As you can see I included the file into quotations. This will help especially if the file path contains spaces. But even if it doesn't, it worth a try.
Please let me know if it did help.
I've tried that but it doesn't work unfortunately... It's really weird as in other functions that I use the same command (different variables) it works and I can write in a set file apart from the main .ini

I would upload the whole thing but it's pretty complicated to go through and understand it, so I rely on it being something as simple as a syntax issue.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:I would upload the whole thing
Please do so.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post by kyriakos876 »

balala wrote:Please do so.
Okay, let's hope jsmorley won't see this as he is strongly against this type of writing deleting stuff to / from a file.

(I warned you about the complexity of this.)

Now to give you and idea, when you install the skin you will see a + button, if you hit that a box will be created via the .lua script. Then, there will appear a - button below that "+" button giving you the option to remove the box you created before. (Don't click the red spot it's a work in progress thing.)

My issue at the moment is line 152 at the .lua script
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:Okay, let's hope jsmorley won't see this as he is strongly against this type of writing deleting stuff to / from a file.

(I warned you about the complexity of this.)

Now to give you and idea, when you install the skin you will see a + button, if you hit that a box will be created via the .lua script. Then, there will appear a - button below that "+" button giving you the option to remove the box you created before. (Don't click the red spot it's a work in progress thing.)

My issue at the moment is line 152 at the .lua script
Not sure, probably I am missing something.
First take care that the [MeasureLua] measure of the Main.ini skin is commented out. This way the measure practically doesn't even exist. Add it back to the code, to can use it.
I did this. In the @Resources\FileToEdit.txt file, existing in the downloaded package, the B1 - B17 variables do exist.
If I remove them, then refresh the skin and click the + and / or - buttons, the variables (in an ascending / descending order) are re-added to the [Variables] section of the FileToEdit.txt file.
You said the !WriteKeyValue bang doesn't work. Take care that this bang writes the appropriate value to the appropriate section of the appropriate file, but this value isn't used by the skin, if you don't refresh it. Every time you're clicking a button, you have to check the content of the FileToEdit.txt file, to see if the variable had been added.
Didn't enter deeply in the code, but I think it is working as it should. Am I wrong?
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post by kyriakos876 »

balala wrote:Not sure, probably I am missing something.
First take care that the [MeasureLua] measure of the Main.ini skin is commented out. This way the measure practically doesn't even exist. Add it back to the code, to can use it.
I did this. In the @Resources\FileToEdit.txt file, existing in the downloaded package, the B1 - B17 variables do exist.
If I remove them, then refresh the skin and click the + and / or - buttons, the variables (in an ascending / descending order) are re-added to the [Variables] section of the FileToEdit.txt file.
You said the !WriteKeyValue bang doesn't work. Take care that this bang writes the appropriate value to the appropriate section of the appropriate file, but this value isn't used by the skin, if you don't refresh it. Every time you're clicking a button, you have to check the content of the FileToEdit.txt file, to see if the variable had been added.
Didn't enter deeply in the code, but I think it is working as it should. Am I wrong?
You got an idea, my issue is that when you click the "-" button the variable B#i# should become 1 again. Try this. Delete everything and click "+" onte time, you will see B1 turn from 1 to 0. When I click "-" this should turn back to 1 but it doesn't. (This is what line 152 does in the .lua or at least is should do that, but it doesn't work if I add the path . If you remove the path it will work and write the B1=1 but in the main .ini file and I don't want it to go there.)
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:You got an idea, my issue is that when you click the "-" button the variable B#i# should become 1 again. Try this. Delete everything and click "+" onte time, you will see B1 turn from 1 to 0. When I click "-" this should turn back to 1 but it doesn't. (This is what line 152 does in the .lua or at least is should do that, but it doesn't work if I add the path . If you remove the path it will work and write the B1=1 but in the main .ini file and I don't want it to go there.)
Yep, you're right. Very interesting. Checking, but for now have no good idea...
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post by kyriakos876 »

balala wrote:Yep, you're right. Very interesting. Checking, but for now have no good idea...
I solved it by moving this command:

SKIN:Bang(string.format('!WriteKeyValue Variables B%s "1" "#@#FileToEdit.txt"', LastAdded))

from line 152 to the very bottom of the function Remove() before the "end". No idea why it works there but not some lines above... (it's no in a loop or anything.. ) weird stuff.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:No idea why it works there but not some lines above... (it's no in a loop or anything.. ) weird stuff.
Yes, you're right and just now I realized what was going on.
The problem is not where the Bang line is, but if it is before or after the file opening, writing and closing section. This is important, because these two parts of the function (the Bang and the file opening-writing-closing) are working with the same file. If the Bang is before the other section, it writes the appropriate values, but when the file is opened, written and closed, the previous modifications are overwritten. If you place the Bang after that section, the Bang overwrites one (or more) variable(s) in the file, so finally you get the appropriate value of those variables.
If you modify the MainLua.lua code to write the variables into the main file (not in the FileToEdit.txt file), as you did, I think, the two operations work on different files and the values written by the Bang aren't modified by the following section of the Remove() function.
For me now it's clear, I think. Stupid mistake, which I didn't realize...
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Using variables in a loop.

Post by kyriakos876 »

balala wrote:Yes, you're right and just now I realized what was going on.
The problem is not where the Bang line is, but if it is before or after the file opening, writing and closing section. This is important, because these two parts of the function (the Bang and the file opening-writing-closing) are working with the same file. If the Bang is before the other section, it writes the appropriate values, but when the file is opened, written and closed, the previous modifications are overwritten. If you place the Bang after that section, the Bang overwrites one (or more) variable(s) in the file, so finally you get the appropriate value of those variables.
If you modify the MainLua.lua code to write the variables into the main file (not in the FileToEdit.txt file), as you did, I think, the two operations work on different files and the values written by the Bang aren't modified by the following section of the Remove() function.
For me now it's clear, I think. Stupid mistake, which I didn't realize...
I understand it now working if the bang is in-between the file opening process, but if you check the file I uploaded, the bang was before the opening of the file... As soon as I put it after the closing command it works. That, I don't understand. It's like the moment the function starts to execute the code it opens the file, writes on it and closes it. But if my bang is BEFORE commanding the open/read file it shouldn't be an issue.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Using variables in a loop.

Post by balala »

kyriakos876 wrote:I understand it now working if the bang is in-between the file opening process, but if you check the file I uploaded, the bang was before the opening of the file... As soon as I put it after the closing command it works. That, I don't understand. It's like the moment the function starts to execute the code it opens the file, writes on it and closes it. But if my bang is BEFORE commanding the open/read file it shouldn't be an issue.
Let's recapitulate: important is the order of the writings:
  • If the Bang is before the open-write-close procedure, the bang writes the appropriate value into the file, but then the open-write-close procedure overwrites what the Bang wrote.
  • If on the other hand, the Bang follows the open-write-close procedure, this procedure writes the file, then the Bang overwrites them with its own values. Because this is what is happening later, the values it writes are kept, remaining written.
  • If you modify the Bang to write the value into the main file (or give a try making it to write the value into any other, old or newly created, file), the open-write-close procedure doesn't have anything to do with the Bang and due to this, the values written by the Bang are kept.