Set variable 'value + 1' not working?

Olof

New member
I have defined a global integer 'Counter' with initial value 0 and want to increase its value by 1. I tried the Set Variable action and selected 'Value + 1', but Counter does not change. What am I doing wrong? This seamed so easy...
 

Endercraft

Moderator (& bug finder :D)
Weird what happens when manually testing the action ?
I don't think this is even possible so there might be a misconfiguration somewhere.
 

Olof

New member
I am new to Macrodroid, but an experienced C# programmer (if I may say so :)). I don't know how to test an action manually. I use a primitive 'if value is so and so, then play a specific sound'.

My problem: I want to launch a navigation app on my Samsung A23 phone, when i start my car engine which means the power to my phone gets connected. On my desk this is working fine, But in my car the launch sometimes fails, probably because of power interruptions during the start of my engine.

The solution I have in mind: Repeat the launch of the nav app if it is not running in the foreground. A loop seems ideal for this. I did not find a loop action, so I created a 'Launch loop' macro (empty triggers, no constraints) which calls itself upon no success Something like this in pseudo code:

global integer giCounter=0

Macro Run (Launch loop)

Macro Launch loop
Set Variable giCounter: (+1)
if {v=giCounter}>3
Cancel Macro Actions (this macro)
end if
Launch nav app
(Wait a few seconds)
if not foreground: (nav app)
Macro Run (this macro)
end if

As you can see, MD should make a maximum of three attempts, but when I try this, it keeps running for ever. Upon inspection giCounter turns out to remain always 0.

All other macros and actions are working fine in my project and when I try it in my car (except for the multiple launch attempts), all is right. The giCounter variable is not used elsewhere in my code.
 

RSF

Well-known member
Upon inspection giCounter turns out to remain always 0.
Is it possible that you also have a local variable called giCounter, within the macro? If so, that'd explain what you're seeing. The value-increment action might be incrementing the local variable, while your if logic is still looking at the global value.

Speaking of local variables, note that their values persist across macro invocations. That's a nice feature of MacroDroid, allowing you to avoid using global variables in many cases.

Actions -> Conditions/loops -> Repeat actions
And that action will allow you to get rid of the giCounter variable entirely (local or global), since it allows you to specify how many times to run as part of its configuration:
Loop.png

Lastly: in general, you might want to avoid macros calling themselves.
 

Olof

New member
Is it possible that you also have a local variable called giCounter, within the macro? If so, that'd explain what you're seeing. The value-increment action might be incrementing the local variable, while your if logic is still looking at the global value.
Speaking of local variables, note that their values persist across macro invocations. That's a nice feature of MacroDroid, allowing you to avoid using global variables in many cases.


And that action will allow you to get rid of the giCounter variable entirely (local or global), since it allows you to specify how many times to run as part of its configuration:
View attachment 5750

Lastly: in general, you might want to avoid macros calling themselves.
You can test manually by clicking on the action then 'Test action'.
Thank you, Endercraft and RSF, for your useful guiding. I guess I must have been too much in a hurry and overlooked the Repeat and Test possibilities, stupid me.

No, I have no other variables named 'giCounter'', but I have several 'old' 'Launch loop' macros using this counter. I disabled them, because I started a new one. Maybe that caused the problem.

I like RSF's elegant code. I am sure I will now be able to solve my problem. Thanks again.
 
Top