It is currently April 27th, 2024, 9:16 am

Help with looping a timer with a randomized value once it hits zero

Get help with creating, editing & fixing problems with skins
miyazakipiiman
Posts: 12
Joined: September 5th, 2023, 9:18 pm

Help with looping a timer with a randomized value once it hits zero

Post by miyazakipiiman »

I've been experimenting with a looping timer that has randomized values and a meter that changes color depending on the percentage of time left. However, I'm struggling to get the timer to loop once it hits zero, and it usually continues into the negatives or returns weird values given my unsuccessful attempts to make it loop. The code is as follows:

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1

[Metadata]
Name=timertest
Author=miyazakipiiman
Information=
Version=
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]

[MeterString]
Meter=String

[Rainmeter]
Update=1000

; this measure is supposed to be random but i have the bounds set to 10 to make it more convenient to test
[MeasureRandom]
Measure=Calc
Formula=Random
LowBound=10
HighBound=10

[MeasureCountdown]
Measure=Calc
Formula=MeasureRandom-Counter
DynamicVariables=1

; the IfTrueAction4 bit of this code is the one I'm struggling with right now
[MeasurePercentLeft]
Measure=Calc
Formula=MeasureCountdown
IfCondition=MeasureCountdown <= MeasureRandom
IfTrueAction=[!SetOption MeterText FontColor "0,255,0,255"]
IfCondition2=MeasureCountdown <= MeasureRandom/2
IfTrueAction2=[!SetOption MeterText FontColor "255,255,0,255"]
IfCondition3=MeasureCountdown <= MeasureRandom/8
IfTrueAction3=[!SetOption MeterText FontColor "255,0,0,255"]
IfCondition4=MeasureCountdown < 0
IfTrueAction4=[!SetOption MeasureCountdown Formula "(MeasureRandom - Counter + Random)"]
DynamicVariables=1

[MeterText]
Meter=String
X=34
Y=74
FontSize=20
FontColor=200,200,0,255
Text=[MeasureCountdown]
DynamicVariables=1
As of right now this code generates a randomized timer with green text above 50%, yellow text below 50%, and red text below 20%. I want it to reset back to 100% when it hits zero but haven't been able to make it do that even after pouring through the documentation and the forums. Ideally I want it to loop indefinitely because I plan on having another skin stop it at a certain point.
Last edited by miyazakipiiman on September 12th, 2023, 7:34 pm, edited 1 time in total.
User avatar
balala
Rainmeter Sage
Posts: 16176
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with looping a timer with a randomized value once it hits zero

Post by balala »

miyazakipiiman wrote: September 12th, 2023, 4:34 pm I've been experimenting with a looping timer that has randomized values and a meter that changes color depending on the percentage of time left. However, I'm struggling to get the timer to loop once it hits zero, and it usually continues into the negatives or returns weird values given my unsuccessful attempts to make it loop. The code is as follows:
I can't follow what is your intention. What you mean by "time left"? Also, as far as I can tell, a looping timer can't have randomized values. A looping timer counts (or counts down) continuously, doesn't return random values. If you want such a counter which counts back (or counts up) from certain value to 0 (or vice-versa), than continuously restarts the countdown / count, your code is way too complicated. Not sure why have you set a Calc measure using a Random function ([MeasureRandom]), especially not one having the same LowBound and HighBound values (this measure is returning a constant value, 10). Also not sure why did you use another Calc measure ([MeasureCountdown] in this case), which has a well-defined formula (which extracts the Counter operator from the value of the [MeasureRandom] measure, which has a constant value), but which is modified by the IfTrueAction4 option of the [MeasurePercentLeft] measure.
So give us please a few precise details on what you want to achieve.
There are a few other inadvertences into your code (for instance a duplicated [Rainmeter] section), but these will be fixed later. First I'd like to understand your intention.
miyazakipiiman
Posts: 12
Joined: September 5th, 2023, 9:18 pm

Re: Help with looping a timer with a randomized value once it hits zero

Post by miyazakipiiman »

balala wrote: September 12th, 2023, 5:56 pm I can't follow what is your intention. What you mean by "time left"? Also, as far as I can tell, a looping timer can't have randomized values. A looping timer counts (or counts down) continuously, doesn't return random values. If you want such a counter which counts back (or counts up) from certain value to 0 (or vice-versa), than continuously restarts the countdown / count, your code is way too complicated. Not sure why have you set a Calc measure using a Random function ([MeasureRandom]), especially not one having the same LowBound and HighBound values (this measure is returning a constant value, 10). Also not sure why did you use another Calc measure ([MeasureCountdown] in this case), which has a well-defined formula (which extracts the Counter operator from the value of the [MeasureRandom] measure, which has a constant value), but which is modified by the IfTrueAction4 option of the [MeasurePercentLeft] measure.
So give us please a few precise details on what you want to achieve.
There are a few other inadvertences into your code (for instance a duplicated [Rainmeter] section), but these will be fixed later. First I'd like to understand your intention.
Okay, so, I have the lowbound and highbound values set to 10 because I was doing trial and error with the IfTrueAction4 option and didn't want to wait longer for the timer to hit 0. I would usually have it be set between 60 and 360 so it would randomly set a timer between 1 and 6 minutes. The other IfTrueActions would set the color of the MeterText depending on the percentage left (so it'd turn yellow at 30 seconds if the timer was 1 minute, or 60 seconds if the timer was 2 minutes). I'm trying to get the timer to reset back to its original, 100% starting value after it hits zero.

You can run the code with changed LowBound and HighBound values in order to make the timer random, I just had them set at 10 so I could experiment easily with the IfTrueAction4 option.

Edit: Also, just set IfTrueAction4 to be blank so you can see what I mean. It keeps counting down into the negatives when I want it to start back up again at 100%.

Code: Select all

;go from this
IfTrueAction4=[!SetOption MeasureCountdown Formula "(MeasureRandom - Counter + Random)"]
;to this
IfTrueAction4=
Edit2:
To be more specific, I'm trying to get the timer to reset back to the original value created by [MeasureRandom]. I can't reset the skin without setting a timer of a different value, and I don't know how to set a Counter back to zero without resetting the skin. I want the [MeasureRandom] value to be random upon booting up the skin but I want the timer itself to loop that random timer every time it reaches zero.

So, if MeasureRandom returned a value of 180, it'd just loop a 3-minute timer indefinitely until I close it, but if it returned a value of 300 it'd loop a 5-minute timer indefinitely until I close it.
miyazakipiiman
Posts: 12
Joined: September 5th, 2023, 9:18 pm

Re: Help with looping a timer with a randomized value once it hits zero

Post by miyazakipiiman »

Nevermind, looked through the documentation again and found a section on counters I wasn't able to find before. The desired result has been achieved.

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1

[Metadata]
Name=timertest
Author=miyazakipiiman
Information=
Version=
License=Creative Commons Attribution - Non - Commercial - Share Alike 3.0

[Variables]
MaxNumber=MeasureRandom
Pause=0
Reset=0

[MeterString]
Meter=String

[MeasureRandom]
Measure=Calc
Formula=Random
LowBound=5
HighBound=10

[cCounter]
Measure=Calc
Formula=cCounter%#MaxNumber#+1

[MeasureCountdown]
Measure=Calc
Formula=MeasureRandom-cCounter
DynamicVariables=1

[MeasurePercentLeft]
Measure=Calc
Formula=MeasureCountdown
IfCondition=MeasureCountdown > MeasureRandom/2
IfTrueAction=[!SetOption MeterText FontColor "0,255,0,255"]
IfCondition2=MeasureCountdown <= MeasureRandom/2
IfTrueAction2=[!SetOption MeterText FontColor "255,255,0,255"]
IfCondition3=MeasureCountdown <= MeasureRandom/8
IfTrueAction3=[!SetOption MeterText FontColor "255,0,0,255"]
IfCondition4=MeasureCountdown > MeasureRandom
IfTrueAction4=[!SetOption MeterText FontColor "0,255,0,255"]
DynamicVariables=1

[MeterText]
Meter=String
X=34
Y=74
FontSize=20
FontColor=200,200,0,255
Text=[MeasureCountdown]
DynamicVariables=1
User avatar
balala
Rainmeter Sage
Posts: 16176
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with looping a timer with a randomized value once it hits zero

Post by balala »

Most probably I'd try to use a Loop measure for count or countdown. It's extremely easy to set its limits. For instance:

Code: Select all

[MeasureCountdown]
Measure=Loop
StartValue=60
EndValue=360
Increment=1
LoopCount=0
If do this, I think both the [MeasureRandom] and the [MeasurePercentLeft] measures become useless and can be removed. The IfConditions set on the [MeasurePercentLeft] measure can be altered a little bit and moved to the [MeasureCountdown] measure (excepting IfCondition4, which can be removed). By this, the measures should look this way:

Code: Select all

[MeasureCountdown]
Measure=Loop
StartValue=60
EndValue=360
Increment=1
LoopCount=0
IfCondition=(MeasureCountdown>[MeasureCountdown:MaxValue]/2)
IfTrueAction=[!SetOption MeterText FontColor "0,255,0,255"]
IfCondition2=((MeasureCountdown>[MeasureCountdown:MaxValue]/8)&&(MeasureCountdown<=[MeasureCountdown:MaxValue]/2))
IfTrueAction2=[!SetOption MeterText FontColor "255,255,0,255"]
IfCondition3=(MeasureCountdown<=[MeasureCountdown:MaxValue]/8)
IfTrueAction3=[!SetOption MeterText FontColor "255,0,0,255"]
DynamicVariables=1
See that in this case, the IfConditions compare the current value of the [MeasureCountdown] measure with fractions of its largest value (represented by the [MeasureCountdown:MaxValue] section variable).
If you want a countdown, you just have to replace the StartValue by StartValue=360, EndValue by EndValue=60, Increment by Increment=-1 and finally the [MeasureCountdown:MaxValue] section variable by [MeasureCountdown:MinValue].