To be honest though, there is no great advantage to defining variables as "local" in our implementation of the interface between Lua and Rainmeter. Each Lua script runs in its own "tablespace", which means that all variables, local or not, are in fact local to that script measure. The only real reason to define a variable as "local" would be to allow using the same variable name in different contexts within the script, and getting different results. I'm not sure I see a good use case for that.
There really isn't a "global-global" construct, in the sense that setting a variable in one Lua Script measure can be seen by a different Lua Script measure. The Lua Script measures are "sandboxed" in their own distinct Lua environments.
It is currently October 9th, 2024, 3:42 am
Trunc(x)/Franc(x) in lua
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
Another use for "local" that I can envision is if you have a function that just does a boatload of calculating, creating a huge number of variables or table entries in order to arrive at a result, and at the end simply "returns" a single value that is the result of all that work.
In that case, making the variables and tables that are only used in the context of that function "local" means that when the function is complete, the variables and tables are "released" from memory, which in an extreme example could be a bit more "memory efficient". It would have to be a boatload indeed though, to make any real difference.
In that case, making the variables and tables that are only used in the context of that function "local" means that when the function is complete, the variables and tables are "released" from memory, which in an extreme example could be a bit more "memory efficient". It would have to be a boatload indeed though, to make any real difference.
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
An example of using "local" that way is:
So numToAdd only exists in the context of Function2(), and isn't maintained outside of that context.
Code: Select all
function Function1()
myVar1 = 5
myVar2 = Function2()
return myVar2
end
function Function2()
local numToAdd = 10 + 10
return numToAdd + myVar1
end
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
Right... I was using "local" as a memory efficiency technique, but I now realize that my script will run for a good 100ms? And does little to no calculations, compared to what can be considered enough to even start to stress a CPU.
I made all my variables global and nothing changed, except for a part where I was using the same variable name while I wanted a different result as you said above. Changed the name to avoid confusing myself in the future and made that global as well. A plus also is that, now, I don't need to pass it in every function that's going to use it, or even worse call it again, adding up unnecessary lines of code.
Thanks, and sorry for any inconvenience I may have caused.
I made all my variables global and nothing changed, except for a part where I was using the same variable name while I wanted a different result as you said above. Changed the name to avoid confusing myself in the future and made that global as well. A plus also is that, now, I don't need to pass it in every function that's going to use it, or even worse call it again, adding up unnecessary lines of code.
Thanks, and sorry for any inconvenience I may have caused.
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
It's all good!kyriakos876 wrote: ↑November 11th, 2018, 3:18 pm Right... I was using "local" as a memory efficiency technique, but I now realize that my script will run for a good 100ms? And does little to no calculations, compared to what can be considered enough to even start to stress a CPU.
I made all my variables global and nothing changed, except for a part where I was using the same variable name while I wanted a different result as you said above. Changed the name to avoid confusing myself in the future and made that global as well. A plus also is that, now, I don't need to pass it in every function that's going to use it, or even worse call it again, adding up unnecessary lines of code.
Thanks, and sorry for any inconvenience I may have caused.
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
I'm not opposed to, nor discouraging, fine tuning for maximum efficiency. There is some charm to that, no matter small the benefit. As long as you understand how local works and doesn't work, I'm all for using it and being as anal-retentive as possible in your code.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
If you don't mind checking this out, I can't figure something out... Please get the skin below load the Calendar.inijsmorley wrote: ↑November 11th, 2018, 4:04 pm I'm not opposed to, nor discouraging, fine tuning for maximum efficiency. There is some charm to that, no matter low small the benefit. As long as you understand how local works and doesn't work, I'm all for using it and being as anal-retentive as possible in your code.
Then open the #@#DaysOfFebruary.lua and check line 110 that says
monthLua = tostring(SKIN:GetVariable('CurrentMonth'))
now that is global so no matter where I put it, I can call it from everywhere.
Now copy that line, delete it, and move it in the initialize function on the very top of the script.
Then on the loaded skin, click the meter that says Update (Dev) and you will see an error in the log that comes back to changing this variable from inside the function to the initialize function. Why is it that when I move this GLOBAL variable I get an error related to it? It's GLOBAL inside the function and it's also GLOBAL in the initialize function which is called whenever the script is called (if I'm not wrong).
Btw, don't mind the syntax error in the log. It works fine even while throwing that.
You do not have the required permissions to view the files attached to this post.
-
- Developer
- Posts: 22843
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Trunc(x)/Franc(x) in lua
Initialize() is run one time, when the skin is refreshed. The important bit is that it is run BEFORE the rest of the skin is updated the first time, so any [Variables] that you create in the skin are not available yet to the Initialize() function. Don't resolve them there. Either resolve them in an Update() function that you set with UpdateDivider=-1 (if they are static variables), or have an OnRefreshAction in the [Rainmeter] section of the skin that calls a GetSkinVars() function or some such.
-
- Posts: 919
- Joined: January 30th, 2017, 2:01 am
- Location: Greece
Re: Trunc(x)/Franc(x) in lua
oooooh okay.... I get it... I will keep it in the function then.jsmorley wrote: ↑November 11th, 2018, 6:10 pm Initialize() is run one time, when the skin is refreshed. The important bit is that it is run BEFORE the rest of the skin is updated the first time, so any [Variables] that you create in the skin are not available yet to the Initialize() function. Don't resolve them there. Either resolve them in an Update() function that you set with UpdateDivider=-1 (if they are static variables), or have an OnRefreshAction in the [Rainmeter] section of the skin that calls a GetSkinVars() function or some such.