It is currently October 13th, 2024, 4:32 pm

Trunc(x)/Franc(x) in lua

Discuss the use of Lua in Script measures.
User avatar
jsmorley
Developer
Posts: 22854
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

To be clear, they are global in the context of that Script measure.

Also note that you can't use a variable in Function1() if it is defined in Function2() and Function2 has not been called yet.
User avatar
balala
Rainmeter Sage
Posts: 16651
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

jsmorley wrote: November 11th, 2018, 12:34 am That isn't exactly what he asked for though. He asked that 23.12 be turned into 23 and 12, not 23 and 0.12. math.floor() is fine for getting the integer part, but not so good for the decimal part.
Oops!!! I missed this small detail I think, but you're perfectly right. Sorry.
jsmorley wrote: November 11th, 2018, 12:34 am If you do in fact want the integer portion as an integer, and the decimal portion as a decimal, there is one simple math function...
Int, Dec = math.modf(myNum)
I didn't know about the math.modf function. It can be useful I think in some cases. Good to know about it.
User avatar
jsmorley
Developer
Posts: 22854
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

balala wrote: November 11th, 2018, 1:44 pm Oops!!! I missed this small detail I think, but you're perfectly right. Sorry.

I didn't know about the math.modf function. It can be useful I think in some cases. Good to know about it.
Well, to be fair, the subject of his post was entirely at conflict with what he asked for in the body of the post.

fract(x) in fact returns the decimal portion of a number as a decimal, so if you go by his post subject, you are right, but in the body he seems to indicate that he wants the decimal portion returned as an integer, which is a horse of a different color.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

jsmorley wrote: November 11th, 2018, 1:20 pm All variables are global in Lua unless you specify local on them.
Sorry if I wasn't clear enough, what I'm asking is if I define a variable to be local in a function, will a function called by the first one be able to use that variable?
User avatar
jsmorley
Developer
Posts: 22854
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

kyriakos876 wrote: November 11th, 2018, 2:28 pm Sorry if I wasn't clear enough, what I'm asking is if I define a variable to be local in a function, will a function called by the first one be able to use that variable?
No, not if you define it as "local". You would then have to pass the value as a parameter to the second function.
User avatar
balala
Rainmeter Sage
Posts: 16651
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

jsmorley wrote: November 11th, 2018, 1:47 pm Well, to be fair, the subject of his post was entirely at conflict with what he asked for in the body of the post.

fract(x) in fact returns the decimal portion of a number as a decimal, so if you go by his post subject, you are right, but in the body he seems to indicate that he wants the decimal portion returned as an integer, which is a horse of a different color.
Right. But it was my inattention (sorry for this), because I know I read he wants the decimal part as integer, but finally when I posted my reply, I forgot about this.
Anyway doesn't matter too much, because kyriakos876 wrote in the meantime that the decimal part as decimal is ok.
User avatar
jsmorley
Developer
Posts: 22854
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

balala wrote: November 11th, 2018, 2:32 pm Right. But it was my inattention (sorry for this), because I know I read he wants the decimal part as integer, but finally when I posted my reply, I forgot about this.
Anyway doesn't matter too much, because kyriakos876 wrote in the meantime that the decimal part as decimal is ok.
He just likes to make extra work for us by not be at all clear and precise about what he is trying to do... The latest "local" question is another example of this... ;-)
User avatar
balala
Rainmeter Sage
Posts: 16651
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Trunc(x)/Franc(x) in lua

Post by balala »

jsmorley wrote: November 11th, 2018, 2:35 pm He just likes to make extra work for us by not be at all clear and precise about what he is trying to do... The latest "local" question is another example of this... ;-)
I don't comment...
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Trunc(x)/Franc(x) in lua

Post by kyriakos876 »

jsmorley wrote: November 11th, 2018, 2:31 pm No, not if you define it as "local". You would then have to pass the value as a parameter to the second function.
Alright cool. Got it.
jsmorley wrote: November 11th, 2018, 1:47 pm Well, to be fair, the subject of his post was entirely at conflict with what he asked for in the body of the post.

fract(x) in fact returns the decimal portion of a number as a decimal, so if you go by his post subject, you are right, but in the body he seems to indicate that he wants the decimal portion returned as an integer, which is a horse of a different color.
I agree, the title might have been a little bit misleading I must say,
kyriakos876 wrote: November 10th, 2018, 8:32 pm And if there's one equivalent to the Franc(x) but not exactly... I want as the end result this:
But yea, mainly because I didn't know how else I to put it, I used this to better describe it.
jsmorley wrote: November 11th, 2018, 2:35 pm He just likes to make extra work for us by not be at all clear and precise about what he is trying to do... The latest "local" question is another example of this... ;-)
Yup, I'm bad at explaining what I want sometimes... I'm trying to make it simple but it turns out complicated.
Pardon me :bow:
User avatar
jsmorley
Developer
Posts: 22854
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Trunc(x)/Franc(x) in lua

Post by jsmorley »

Here is how it works:

If you have a skin like this:

Code: Select all

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

[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterResult]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=[&Lua:Function1()]
And Lua like this:

Code: Select all

function Function1()

		myVar1 = 5
		myVar2 = Function2()
		
		return myVar2
		
end

function Function2()

	return 20 + myVar1

end
It works fine and returns "25" to the skin.

If you have Lua like this:

Code: Select all

function Function1()

		local myVar1 = 5
		myVar2 = Function2()
		
		return myVar2
		
end

function Function2()

	return 20 + myVar1

end
That is an error, as myVar1 does not exist in the context of Function2().

You would have to:

Code: Select all

function Function1()

		local myVar1 = 5
		myVar2 = Function2(myVar1)
		
		return myVar2
		
end

function Function2(arg)

	return 20 + arg

end
In order to pass the "local" value of myVar1 to Function2(arg)