It is currently January 23rd, 2022, 9:03 am

## Creating Independent Counters

Tips and Tricks from the Rainmeter Community
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

### Creating Independent Counters

This method of creating individual counters has been around for a bit but I decided that they should be written up as there are a few tricks that I've figured out. The advantages of using independent counters are numerous and include the ability to increase their amount by any number you want and being able to pause or reset their values.

NOTE:
This tips 'n trick thread heavily uses the Modulo operator (i.e. %), otherwise known as Remainder Division. It's important to understand how this operator works before continuing.

First we're going to create some variables that we are going to use later on.

Code: Select all

``````[Variables]
MaxNumber=30
Pause=1
Reset=1``````
Now we need to create the counter.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter+1``````
The reason this works is that when calling a measure the value from the last update cycle is used. With this in mind, by calling the measure inside itself we can easily change it's value.

Note: When naming your counter always remember that there is a built in Variable/Function named Counter so yours should be named something else.

Making the counter reset at a given value
Having it reset after a given value is easy and is accomplished the same way as if you were using the Counter variable.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=(cCounter+1)%(#MaxNumber#+1)``````
This will have the counter reset to zero after it hits 30, which is what we have MaxNumber set to. We have the measure add 1 to MaxNumber so that it will reset after the value else it will reset at that value.

If you want your counter to reset to one instead of zero we just need to move things around a bit.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter%#MaxNumber#+1``````
With this method we also don't need to worry about adding one to the reset value.

Resetting a counter via Mouse Actions
How about having it reset to zero when we click on the skin.

Code: Select all

``````[Rainmeter]
LeftMouseUpAction=!SetVariable Reset 0

[cCounter]
Measure=Calc
Formula=(cCounter+1)*#Reset#
IfBelowValue=1
IfBelowAction=!SetVariable Reset 1
DynamicVariables=1``````
Multiplying the counter by zero will easily reset it. Just make sure to set an action to set the Reset variable back to one when you're done.

Ghost points out that if you want to reset with MouseOver there is a much nicer way without using a variable.

Code: Select all

``````[Rainmeter]
MouseOverAction=!DisableMeasure cCounter
MouseLeaveAction=!EnableMeasure cCounter

[cCounter]
Measure=Calc
Formula=cCounter+1``````
Note: This method only works if the counter is disabled for at least one update cycle.

Now we can combine a couple methods of resetting to have the counter count up, reset, and wait for us to tell it restart again.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter+1
IfEqualValue=#MaxNumber#
IfEqualAction=!DisableMeasure #CURRENTSECTION#``````
When we want the counter to start counting again we just need to use !EnableMeasure cCounter.

Pausing a counter
Lets pause the counter when you mouse-over the skin.

Code: Select all

``````[Rainmeter]
MouseOverAction=!SetVariable Pause 0
MouseLeaveAction=!SetVariable Pause 1

[cCounter]
Measure=Calc
Formula=cCounter+#Pause#
DynamicVariables=1``````
When the MouseOver action is triggered you stop adding 1 to the counter yet it retains it's value. When the mouse leaves the skin you start adding 1 to it again.

We can also have the counter stop counting at a certain value and stay there.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter+(cCounter<#MaxNumber#)``````
What we did was added a conditional statement. Conditional statements without an "if ? then : else" format resolve to either 1 for true or 0 for false. Using this fact we can easily use conditional statements in mathematical formulas.

Triggering Actions with a Counter

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter+1
IfAboveValue=#MaxNumber#
IfAboveAction=[!DisableMeasure #CURRENTSECTION#]["Some Action"]``````
This will count up to the MaxNumber, disable itself so it doesn't continue to count, and execute some action such as opening an addon. This method can be used to execute some action a certain number of updates after skin is loaded.

We can use the built in Counter function/variable to accomplish the same thing. Please note that Counter returns the number of updates since the skin was initially loaded and only returns to zero when Rainmeter is loaded or a !RefreshApp bang is used.

Code: Select all

``````[cCounter]
Measure=Calc
Formula=Counter
IfAboveValue=#MaxNumber#
IfAboveAction=[!DisableMeasure #CURRENTSECTION#]["Some Action"]``````
Note: To execute a set of commands when the skin is loaded or refreshed, without using a counter, use OnRefreshAction=!SomeAction in the [Rainmeter] section of the skin.

Bonus Lesson: Making a counter that counts down, not up

Code: Select all

``````[cCounter]
Measure=Calc
Formula=(cCounter-1+(#MaxNumber#+1))%(#MaxNumber#+1)``````
This one is a bit harder to explain. What we are doing is taking the value of the counter and subtracting 1. Then we have to add the value of our MaxNumber else we'll just end up with a negative value. Now we have to take the remainder of that value using Modulo to get our final value.
Last edited by smurfier on October 10th, 2011, 7:07 pm, edited 7 times in total.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
jsmorley
Developer
Posts: 22450
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

### Re: Creating Independent Counters

smurfier wrote:This method of creating individual counters has been around for a bit but I decided that they should be written up as there are a few tricks that I've figured out. The advantages of using independent counters are numerous and include the ability to increase their amount by any number you want and being able to pause or reset their values.
Added, thanks for the excellent contribution:
http://rainmeter.net/cms/Tips-CountersGuide
GHOST®
Posts: 55
Joined: March 11th, 2011, 6:33 pm
Location: Garden City, MI

### Re: Creating Independent Counters

smurfier wrote:This method of creating individual counters has been around for a bit but I decided that they should be written up as there are a few tricks that I've figured out. The advantages of using independent counters are numerous and include the ability to increase their amount by any number you want and being able to pause or reset their values.

Now lets pause the counter when you mouse-over the skin.

Code: Select all

``````[Rainmeter]
MouseOverAction=!SetVariable Pause 0
MouseLeaveAction=!SetVariable Pause 1

[Variables]
Pause=1

[cCounter]
Measure=Calc
Formaul=cCounter+#Pause#
DynamicVariables=1``````
When the MouseOver action is triggered you stop adding 1 to the counter yet it retains it's value. When the mouse leaves the skin you start adding 1 to it again.

Code: Select all

``````[Rainmeter]
MouseOverAction=!DisableMeasure cCounter
MouseLeaveAction=!EnableMeasure cCounter

[cCounter]
Measure=Calc
Formaul=cCounter+1
DynamicVariables=1
``````
Saves a Variable. ;o)

Though I'm kixing myself in the arse on the Counter Reset to 0. Was beating my head against the wall for hours the other day & NEVER even thought about multiplying my counter by 0 to do so. Thanks for that one for sure. :oD
Last edited by GHOST® on July 3rd, 2011, 9:16 pm, edited 1 time in total.
"Do you want to be healed, now? Or would you prefer to bleed to death so I can try my hand at resurrection?"
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

### Re: Creating Independent Counters

While disabling then enabling the measure does certainly work for mouseovers, it does not work so nicely for something like a button click.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

### Re: Creating Independent Counters

Thanks for this smurfier!

Counters are something I've never been able to wrap my head around very well - this helps a great deal. I never could have thought this up myself, but some of it does seem startlingly obvious after seeing it in front of me.
Flying Hyrax on DeviantArt
GHOST®
Posts: 55
Joined: March 11th, 2011, 6:33 pm
Location: Garden City, MI

### Re: Creating Independent Counters

smurfier wrote:While disabling then enabling the measure does certainly work for mouseovers, it does not work so nicely for something like a button click.
Doh!! Good point! Musta skipped over that part when first reading your post. :o(

Anyways, thanks for these awesome tips. :)
"Do you want to be healed, now? Or would you prefer to bleed to death so I can try my hand at resurrection?"
Seahorse
Posts: 1175
Joined: June 9th, 2010, 5:56 pm
Location: Locks heath, UK

### Re: Creating Independent Counters

Is there a way of stopping a counter when it reaches a number?
"Regrettably your planet is one of those scheduled for demolition"
Mike

My Skins at DeviantArt

KreAch3R
Posts: 608
Joined: February 7th, 2011, 7:27 pm
Location: Thessaloniki, GR

### Re: Creating Independent Counters

Try something like that:

Code: Select all

``````[cCounter]
Measure=Calc
Formula=cCounter+1
IfEqualValue=#Number#
ifEqualAction=!DisableMeasure cCounter
DynamicVariables=1
``````
Inactive, due to life changes. Send me a PM for any question.

Desktop DeviartArt
Seahorse
Posts: 1175
Joined: June 9th, 2010, 5:56 pm
Location: Locks heath, UK

### Re: Creating Independent Counters

Cheers, that does it, now I need to go scratch my head some more...
"Regrettably your planet is one of those scheduled for demolition"
Mike

My Skins at DeviantArt

KreAch3R
Posts: 608
Joined: February 7th, 2011, 7:27 pm
Location: Thessaloniki, GR

### Re: Creating Independent Counters

Glad it worked. Messing with counters for the first time needs some head scratching indeed.
Inactive, due to life changes. Send me a PM for any question.

Desktop DeviartArt