It is currently March 29th, 2024, 8:02 am

If Then Else How?

Introduce yourself to the Rainmeter community!
SolihullRog
Posts: 2
Joined: December 30th, 2018, 12:15 am

If Then Else How?

Post by SolihullRog »

Hi,

After more than 40 years in the business - since writing my first code - I've recently discovered RainMeter.

I find it an intriguing product - for both positive and negative reasons.

I've read all the blurb, and Getting Started, and looked at lots of available downloads, but I find educational information a bit lacking (for me).

Having read Getting Started I feel as though "You are now on your own".

I've written a Rainmeter launcher for my 50+ apps. It consists of a single skin and about 50 String Meters and 700 lines of script.

I now want to streamline it - to (a) make it simple to amend, and (b) to publish it with lots of guiding tips.

But I struggle to understand how to code an If ... then ... action1 ... else... action2 situation.

I've created a template Meter to be used for every button on my launcher. I want to be able to replicate it 49 times to make a skin of 50 buttons.

I would then make a couple of adjustments to each button (to its text and its LeftMouseUp action), and have a fully working / easily modifiable skin.

So, simplifying,...

If I only had six buttons on my launcher, and I want them positioning at (top row) column 1 - at 30,60 column 2 - at 100,60, and column 3 at 170,60.

Then row 2 column 1 - at 30,100 column 2 - at 100,100, and column 3 at 170, 100.

I plan to do this by each String Meter beginning with R=x and C=y where R is the row, and C is the column.

So my 6 String Meters would begin (on the first button) R=1, C=1 then (on the second button) R=1 and C=2, then (on the third button) R=1 and C=3. Then R=2, C=1 etc...

'Somewhere' (in the String Meter?) I would have code containing something like:

If C=1 then X = 30. If C=2 then X=100. If C=3 than X=170.
If R=1 then Y=60. If R=2 then Y=100.

But on a much greater scale.

My researches fail to help me with the following:

1. Can I do all this in a String Meter?
2. If not, in what kind of Meter do I do it?
3. Is 'Calc' a 'controlled' word?
4. Which decision-making verb do I use?
5. Which action do I use to set each of X or Y?

Glad to be here ...
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: If Then Else How?

Post by jsmorley »

The first problem is the assumption that Rainmeter is a procedural programming language, like C or PHP or Javascript or something. It isn't.

In essence, Rainmeter skins are executed in a single big "loop" that is controlled by the Update value in the [Rainmeter] section of the skin.

During this loop, it does the following:

1) Updates current value of all measures.
2) Tests any "conditions" that are on the measures, like IfCondition, IfMatch, IfEqual and the like.
... If the result of these tests change from false to true, then any corresponding "true" actions, normally one or more bangs, are executed.
... if the result of these tests change from true to false, then any corresponding "false" actions, normally one or more bangs, are executed.
... Note that these actions can include !SetOption bangs, which can be used to set options on meters.
3) Executes any OnChange or OnUpdate actions on the measures. These are also normally one or more bangs.
4) Updates the current state of all meters. Has them get the latest value from bound measures, update any dynamically changing options that are caused by !SetOption bangs, or based on formulas or dynamic variables, etc.
5) Redraws the entire skin.
6) Starts over with 1)

ANY time a mouse action is detected on a meter or the overall skin background, any corresponding actions, again normally one or more bangs, are executed. Mouse actions are interrupt / event-driven, and happen immediately.



There is no for / next method of creating meters.
Meters are not, and cannot be, created dynamically. You hard code the entire skin, all measures and all meters, up front. If you want for instance a user to be able to "add" or "remove" meters, you would have to create more of them than you would likely ever need, and just "hide" and "show" them as needed.

There are no If / then / else constructs on meters. There actually is no if / then / else construct outside of formulas anywhere in Rainmeter. At most there is an if / then idea in "if" tests on measures. There are ternary calculations in formulas, which while entirely numeric, can achieve an if / then / elseif / then / else result in the context of a numeric option.

Meters are simple creatures. They look like:

[MeterName]
Meter=type
MeasureName=some measure name
option1=value
option2=value
option3=value
...

That's it. Now those "values" can by dynamically changed by actions on measures, or by a mouse action, which gives you a lot of flexibility, but the meter itself if pretty dumb. It just draws the "type" of meter defined, using the current value from MeasureName, and the "options" and their "values" defined.

Calc is not a reserved word as such, however, it is a type of measure when used with Measure=Calc. There are no reserved words in Rainmeter. That should tell you something about the nature of Rainmeter. There is just no need to reserve works like IF THEN ELSE ELSEIF DO WHILE UNTIL FOR NEXT BREAK GOTO or any others, because the kinds of things those words control in other languages don't exist.

There is no such thing as a "decision-making verb" in Rainmeter. Rainmeter has no need of verbs. There is no "flow control" in Rainmeter. The "flow" is simply that loop above. Period.

There is literally no code that is not part of a measure or a meter. That's all Rainmeter is. Measures and meters.

This is a bit of a simplistic explanation, with the hope of having the basic concept sink in before you get into all the "exceptions" and "tricks" you can play to force Rainmeter to draw outside the lines a bit. Someone is going to pipe in with "you can use a Calc measure with a ternary formula to create an If/then/else construct that can be used with IfCondition to set options, and you can do all kinds of procedural programming in Lua", but they are going to just be trying to successfully annoy me and possibly confuse you.

It is not my intention to discourage, far from it. Rainmeter is very powerful, and can be made to do a boatload of very clever things. My warning is that you can't safely come to it with pre-conceived ideas based on programming you may have done in the past. Rainmeter is not a programming language. Many of the constructs that would be basic in a language like C just don't exist in concept. If you can wrap your head around that "loop" I described above, then explore the many, many bangs that are available to take actions based on results of measures or mouse actions, and the many, many options on meters that can be dynamically changed by them, you will find that you can be pretty creative.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: If Then Else How?

Post by balala »

I'd add just two minor detail to jsmorley's reply:
  • jsmorley wrote: December 30th, 2018, 12:46 am 2) Tests any "conditions" that are on the measures, like IfCondition, IfMatch, IfEqual and the like.
    ... If the result of these tests change from false to true, then any corresponding "true" actions, normally one or more bangs, are executed.
    ... if the result of these tests change from true to false, then any corresponding "false" actions, normally one or more bangs, are executed.
    ... Note that these actions can include !SetOption bangs, which can be used to set options on meters.
    ... if the result of these tests don't change, but an IfConditionMode=1 or IfMatchMode=1 option is set on measure, the appropriate bang(s) are executed on each update cycle.
  • jsmorley wrote: December 30th, 2018, 12:46 am There is literally no code that is not part of a measure or a meter. That's all Rainmeter is. Measures and meters.
    Plus a few, let's name them "special" sections, like [Rainmeter], [Variables] and [Metadata]. These sections are nor meters, nor measures. Each of them has a well defined role.
SolihullRog
Posts: 2
Joined: December 30th, 2018, 12:15 am

Re: If Then Else How?

Post by SolihullRog »

Hi folks,

And thanks for your replies.

I have always believed that the fault for any failure to communicate is shared equally between the instigator and than the responder, so I apologise for any mis-communication on my behalf.

But, being very conversant with procedural and object-oriented development (although I tend not to use O-O terminology), having managed multi-national O-O-based system-builds for thousands of users, and having spent a few hours building a fully functional launcher for about 50 apps, I was in full agreement with almost all of jshuntley's post. I appreciate your efforts.

I didn't learn anything from the post (although I thought it would be useful in the manual), but two things puzzled me.

1. "Meters are not, and cannot be, created dynamically." I wonder why this was quoted, when I would have expected it to be obvious in most pieces of software. (Probably because you didn't understand what I was trying to do?).

2. "There actually is no if / then / else construct outside of formulas anywhere in Rainmeter." I found this puzzling after having read the major post at https://forum.rainmeter.net/viewtopic.php?f=118&t=15801

Apart from that, thanks for your answers to my questions.

My plans have now changed, and I wish you well during 2019.