It is currently March 29th, 2024, 6:49 am

Swapping nested variables help

Get help with creating, editing & fixing problems with skins
User avatar
Cariboudjan
Posts: 264
Joined: May 12th, 2019, 8:55 am

Swapping nested variables help

Post by Cariboudjan »

RightMouseDownAction swaps variables with nest. (Doesn't work)
LeftMouseDownAction swaps variables normally. (Works)

Both actions do the same thing. They swap PositionA and PositionB variables. When the variables are swapped normally, (#PositionA# with #PositionB#) everything works normally. When the variables are swapped nested ([#Position[#SlotName1]] with [#Position[#SlotName2]]) it doesn't work. The first variable swaps fine, but since the second variable refers to the first variable, and since nested variables adjust dynamically for each bang, RightMouseDownAction causes both numbers to match rather than swap.

So regularly, A=B and B=A (A-B)
But nested, we get A=B and B=B (B-B)

GIF 5-30-2020 2-21-43 PM.gif
How do you swap nested variables?

Code: Select all

[Rainmeter]
MiddleMouseUpAction=[!Refresh]

[Variables]
SlotName1=A
SlotName2=B

PositionA=1
PositionB=2

[Meter]
Meter=String
StringAlign=CenterCenter
SolidColor=255,55,55
FontSize=25
Text=#PositionA# - #PositionB#
X=100
Y=100
W=200
H=200
RightMouseUpAction=[!SetVariable PositionA [#Position[#SlotName2]]][!SetVariable PositionB [#Position[#SlotName1]]][!Update]
LeftMouseUpAction=[!SetVariable PositionA #PositionB#][!SetVariable PositionB #PositionA#][!Update]
DynamicVariables=1
You do not have the required permissions to view the files attached to this post.
User avatar
Brian
Developer
Posts: 2674
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: Swapping nested variables help

Post by Brian »

This is a common programming scenario that comes up. While some "languages" provide automatic swapping of variables or object, most of them end up using a temporary variable/object to accomplish this.
https://en.wikipedia.org/wiki/Swap_(computer_programming)#Using_a_temporary_variable

So in Rainmeter, using the nested variable syntax in an action basically means that variable will always be dynamic.
In your case, this means after the first bang [!SetVariable PositionA [#Position[#SlotName2]]], both PositionA and PositionB will equal 2.

It works the "old" way because the old syntax is only dynamic during the update cycle, and not "in-between" bangs.
See the "Notes" here: https://docs.rainmeter.net/manual/variables/nesting-variables/

So to fix your issue, you will have to either use the old syntax, or create a 3rd "temporary" variable like this:

Code: Select all

[Rainmeter]
MiddleMouseUpAction=[!Refresh]

[Variables]
SlotName1=A
SlotName2=B

PositionA=1
PositionB=2
PositionTemp=

[Meter]
Meter=String
StringAlign=CenterCenter
SolidColor=255,55,55
FontSize=25
Text=#PositionA# - #PositionB#
X=100
Y=100
W=200
H=200
RightMouseUpAction=[!SetVariable PositionTemp [#Position[#SlotName1]]][!SetVariable PositionA [#Position[#SlotName2]]][!SetVariable PositionB [#PositionTemp]][!Update]
LeftMouseUpAction=[!SetVariable PositionA #PositionB#][!SetVariable PositionB #PositionA#][!Update]
DynamicVariables=1
-Brian
User avatar
Cariboudjan
Posts: 264
Joined: May 12th, 2019, 8:55 am

Re: Swapping nested variables help

Post by Cariboudjan »

Thanks Brian.