How to assign a unique variable to dynamically created component?

I’m making buttons dynamically on one page, I’m essentially trying to make a new app variable each time a button is created by combining 2 initialized app variables

Idea:
intialized app variable name = var = " ";
intialized app variable name = i = 0;
button created - assigned to var1
button created - assigned to var2
button created - assigned to var3

so on and so on, but I’m begging to think this isn’t possible to do in thunkable. If it is;t possible to do this then how can I make and assign a button to a variable when it’s dynamically created with thunkable?

Picture of code snippet:

1 Like

Use this variable blocks

Screenshot_20210113_063322

Instead of the “name” put your dynamic values in text join to make them as a new variable.

2 Likes

Thanks for providing both a detailed description of what you are trying to do as well as the blocks you are using. Both are incredibly valuable to the community to make suggestions.

@muneer correctly indicates it is possible, but it may not be advisable. What is your goal? Are you trying to create some sort of array or data-bound form? Depending on the use, there may be a thunkable component better suited to your purpose.

4 Likes

I agree with @drted. I was going to say that I’m always confused about why people would want to use dynamically-named variables but there may be a use case I just haven’t come across.

A list, data source or firebase is almost always going to be more manageable and efficient.

2 Likes

@drted and @tatiang

Hi! so in more detail what I’m trying to do is this:

Page 1: So on the first page, the user can make buttons dynamically and that button is stored in a variable with a unique name.

Page 2: when one is clicked they go to this page where the variable of the dynamically created button is referenced, a timer starts on that page and when the user presses stop on the timer, the time of the timer when it was stopped, is set to the text of the clicked button on the first page.

Would a data source of firebase be better in this case?

1 Like

No, I think you’re probably going about it right. There are multiple ways to do this but using the blocks @muneer provided should work.

2 Likes

Hi @muneer,

Thanks for all your help! I think I followed your instructions but I think I didn’t do it right here is what it looks like:

And I got this for the variable names:
Screenshot 2021-01-14 at 6.56.18 PM (1)

I did something wrong?

what you want the text to be?

The component block stores the component’s ID. The ID is a long string of characters like in the screenshot you posted above. You need the button component’s Text. So your blocks would be set app variable “Time” to [get button [component] text].

2 Likes

As @tatiang described, Thunkable uses an internal ID for every component you create. For example, right click on any component and you will see a good example of internal IDs of Thunkable in the pop-up menu.
image

Another issue is the names you assign to the buttons. I take it you are creating a variable called “Time” in this variable you want to store a text followed by a number. so you need to do something like this
image
Which, if the text of the button is “Car” then the variable will have the text “Car1”. The way you have done it would yield to trying to increment the the value by 1.

3 Likes

Thank you!

Thank you!
One question: how would I reference a variable that was created?

1 Like

Just to clarify, let’s use a sample from the blocks you created

This does not mean to get the value of variable “Time” but it actually means you are getting the value of the variable mentioned in variable “Time”.

So if the newly created variable is called “var1” which is referenced by the dynamic variable “Time” then your block will obtain the value of “var1”.

Hope this clarifies the situation.

1 Like