I need some help with variable.
I would like to use the value of a variable as a list’s name to be able to modify it.
For example : i have two players and i need to change the amount of colored balls owned by the players. Each player has a dedicated button. Same for each ball. And when i click on one of these it changes by 1 the amount of ball of the selected player.
So :
For each player i create a list with item 1 = name, item 2 = number of red ball, item 3 = number of green ball, etc.
First, i select the player.
Then i click on a ball’s button to change the number by 1.
Right now, i can do this by using “if…do” control. But there are too many as the number of player increase and slow down the app.
My wish is to set a variable in the name of player’s list when i select a player.
Then, when i click to modify the amount of ball, call that variable as the list’s name.
Below the blocks of what i have in mind. As it is, it does’nt work.
Maybe i am misusing the variables. I will appreciate any help.
Thank your for your time.
Yes it is possible, but I think there may be a better approah for you app.
First, how to refer to variables by their name. At the bottom of the variables section, there are some funny looking blocks that allow you to get and set variables by their name.
But for hierarchial data (properties with properties, lists of list, etc.) JSON objects are the way to go. Here is one (of many ) example on how to read and create JSON hierarchies.
I’m all about JSON, so if you search for my name and JSON in the list, you will find a lot of examples. Many of the examples cover WebAPI calls, but they will show the techniques for looping thrrough data, checking for values, sorting, filtering, etc.
About JSON objects i don’t think i have the capacity to understand it correctly at this point. I will get to it when i am more comfortable with thunkable.
So I use the block you refered to. Here are my blocks after use.
Not sure if I used it right but I managed to make it work.
It’s like copying to a temporary list, the initial list you want to modify.
I just have to “recopy” the temporary list to the initial one at the end. Correct ?
JSON is the probably the fastest way to do this. Just to add… what you’re really considering is the difference between an “array” and a “record” in programming. An array typically stores one type of value such as scores for each player, while a record stores different types of values (e.g. name, ID, score).
In Thunkable, lists can hold array data but for records like you’re wanting, it’s usually best to use a data source. You can create a blank data source in Thunkable and give the columns the titles you need (name, red number, green number, etc.) and then use the Data Source blocks to get and update the data.