• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Create a (global) variable with the name of another variables value?

DoctorCat

Lurker
Oct 9, 2015
5
1
Hello all.

I have a scene with buttons. When I press a button, the tap action is Perform task. %par1 contains %element_name. Now, I would like to create a global vairable with the name of this value.

The purpose is to make a general task that will work for a lot of different buttons in the scene, without neccessarily knowing the variable names.

Example:
I press Button1. That will Perform task with %par1 set to %element_name (i.e Button1)
Now, I'd like the task to create a new global variable with the name %Button1.

But how?

Please explain it in a simple way. Even though I've played quite a bit with Tasker, I am not a professional or a programmer, just learning on the way.
 
Welcome to Android Forums.

Do you really need to create a variable called %Button 1?

Perhaps you mean you want to assign Button1 as the value to a global variable like ... in the Tap of each button you have ...
Variable Set %Waaa to %element_name
... and when the Scene returns control to the Task you have something like ...
Flash %Waaa

If it was in a button called Button1 then the Flash would say Button1.

... Thom
 
Upvote 0
Welcome to Android Forums.

Do you really need to create a variable called %Button 1?

Perhaps you mean you want to assign Button1 as the value to a global variable like ... in the Tap of each button you have ...
Variable Set %Waaa to %element_name
... and when the Scene returns control to the Task you have something like ...
Flash %Waaa

If it was in a button called Button1 then the Flash would say Button1.

... Thom

Hello, and thanks for the welcome and quick reply! I would actually like to create a variable with that name because it should be associated with that button, it will come in handy later on. I also would like to know if this is even possible. I've been thinking of this before but was never able to figure it out.
 
Upvote 0
I am confused as to your objective. If you have a variable called Buttom1. what would you store in it?

I have a profile that has 8 scenes. I created a set for larger screens and a set for smaller screens. When the Profile starts it tests the screen size and loads the names of the appropriate set of 8 into an array. I then have all scene references in all tasks and scenes to the array like %Example1.

... Thom
 
Last edited:
Upvote 0
Seems like some parts of your comment was lost. I am not sure if your example will do the same thing. If every button set %element_name to %Waaa, won't the value of %Waaa be overwritten each time I press a button?

And that list, what is that, and how do you access it and associate elements with it?

As I said earlier, I am a beginner of this. I thought it would be easy if I could create an unique variable (if not already created) when it's needed.

The %Button1 variable would in this case store info related to that button, like if it's enabled. The thing is that I am lazy and would like to reuse the same task for all buttons, and not predefine variables if I can avoid it. At the same time I would like it to be easy to refer to stuff, and if I use the same naming logic for things I will understand what I did next week too... hopefully. (That doesn't really have anything to do with this, but this way I get the same name for both scene element and variable automatically).

I find lot's of examples to create a variable with the value of another variable, but not to set the actual %Name of a new variable with the value of another var.

So just for the sake of it, is this even possible to do in Tasker, even if it may not be the best solution?

And I really appreciate that you're trying to help me, and understand if it's frustrating to explain stuff to someone who just fairly know what he's doing... :)
 
Upvote 0
There is a capability in Tasker that addresses the objective you are describing ... Clone.

You have a profile, task, or scene ... clone it and assign a new name ... tweak it to incorporate the difference.

For the 16 scenes I mentioned ... two were original ... 14 were cloned and tweaked from those two.

Dynamically creating a variable with an arbitrary name is not something I am aware can be done. You could almost do it with an array knowing implicitly what the relative position in the array means and then reference it by the item name of the array.

You can create an array with values separated by commas an d with an instruction Split that into individual variables. ...
%E with a,b,c
... could become ...
%E1 with value a,%E2 with value b, %E3 with vale c.

The approach that works with Tasker ... start out small ... chip away at it.

... Thom
 
Last edited:
Upvote 0
See if this helps ...

A_task
A1: Show Scene [ Name:A_scene Display As:Dialog Horizontal Position:100 Vertical Position:100 Animation:System Show Exit Button:Off Continue Task Immediately:On ]
A2: Wait Until [ MS:0 Seconds:0 Minutes:0 Hours:0 Days:1 ] If [ %A_variable Set ]
A3: Destroy Scene [ Name:A_scene ]
A4: Flash [ Text:%A_variable Long:On ]
A5: Variable Split [ Name:%A_variable Splitter:, Delete Base:Off ]
A6: Flash [ Text:%A_variable1 Long:Off ]
A7: Flash [ Text:%A_variable2 Long:Off ]
A8: Flash [ Text:%A_variable3 Long:Off ]
A9: Variable Clear [ Name:%A_variable Pattern Matching:Off ]

... the Tap and Long Tap functions say ...

A1: Variable Set [ Name:%A_variable To:%scene_name,%element_name, %event_type Do Maths:Off Append:Off ]
A2: Stop [ With Error:Off Task: ]

... Thom
 
Upvote 0
Thanks for your examples Thom.

However, I solved this myself. I didn't understand the section under variables called Variable reference in the Tasker user guide.

If I set %par1 to %scene_element and then set %%par1 to something, this will create a variable named after the contents of %par1.

Incredible simple and right under my nose, but it took me a loooong time to understand that this was what I needed.

So yes - it is certainly possible to do this.
 
Upvote 0
Hello again!

Here is an example:

I have a couple of custom buttons in a scene. I want to be able to enable them, i.e. change background color or something similar, but I want one single task to do this.

Element names is Button1, Button2, Button3 etc.
Element backgrounds is set to %Button1, %Button2, %Button3 etc.
%Enabled is set to a color.
%Disabled is set to a color.
The tap action of each button is set to perform the same task.
%par1 is set to %element_name.

---

Perform task;

If %%par1 does not match #Enabled

Variable set %%par1 to %Enabled

else

Variable set %%par1 to %Disabled

---

= When a button is tapped, %par 1 is set to %element_name; i.e. Button1, Button2, Button3 etc.

I can then use %%par1 to create a variable name from the value of %par1. This way I can use the same script for all buttons without specifying anything special. Maybe there is some simpler way achieve this, but I could not figure it out, I am a newbie after all! :)
 
  • Like
Reactions: Thom
Upvote 0
Was this the point?

AA
A1: Variable Set [ Name:%Enabled To:#FF00FF00 Do Maths:Off Append:Off ]
A2: Variable Set [ Name:%Disabled To:#FFFF0000 Do Maths:Off Append:Off ]
A3: Variable Set [ Name:%Button1 To:%Enabled Do Maths:Off Append:Off ]
A4: Variable Set [ Name:%Button2 To:%Enabled Do Maths:Off Append:Off ]
A5: Variable Set [ Name:%Button3 To:%Enabled Do Maths:Off Append:Off ]
A6: Show Scene [ Name:AA Display As:Dialog Horizontal Position:100 Vertical Position:100 Animation:System Show Exit Button:On Continue Task Immediately:Off ]

--- --- ---

AA-Flash
A1: If [ %%par1 ~ %Enabled ]
A2: Variable Set [ Name:%%par1 To:%Disabled Do Maths:Off Append:Off ]
A3: Else
A4: Variable Set [ Name:%%par1 To:%Enabled Do Maths:Off Append:Off ]
A5: End If

--- --- ---

Scene: AA
P:1024x512 L:-1x-1

Orientation: System
Background Colour: #33FFFFFF
Action Bar Style: System
Title: AA
Subtitle:
Icon: null
Tab Labels:

Element: Rectangle1/Rectangle
Geometry:
P:0,0 1024x512 L:0,0 1017x1015
Content:
Shader: None
Colour: #FF000033
End Colour:
Border Width: 2
Border Colour: #FFFFFF00
Corner Radius: 30
Rounded Corners: All
Events:
Click: 280

Element: Button1/Button
Geometry:
P:64,128 256x256 L:324,51 315x315
Content:
Label: B1
Label Size: 16
Label Width Scale %: 100
Label Colour: %Button1
Font:
Position: Centre
Icon: null
Events:
Click: 246

Element: Button2/Button
Geometry:
P:384,128 256x256 L:324,424 315x315
Content:
Label: B2
Label Size: 16
Label Width Scale %: 100
Label Colour: %Button2
Font:
Position: Centre
Icon: null
Events:
Click: 251

Element: Button3/Button
Geometry:
P:704,128 256x256 L:352,352 315x315
Content:
Label: B3
Label Size: 16
Label Width Scale %: 100
Label Colour: %Button3
Font:
Position: Centre
Icon: null
Events:
Click: 253

--- --- ---

Each Tap section
A1: Variable Set [ Name:%par1To:%element_name Do Maths:Off Append:Off ]
A2: Perform Task Name AA-Flash Parameter 1 %par1


... Thom
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones