Local variable in an event handler

image
How can I creat this local variable (x2) in Thunkable? TIA

The most “local” scope in Thunkable is APP. For anything but the most basic/educational apps, this is a massive shortcoming. If you use the narrowest scope (app) and have 10 variables in each screen in an app with 7 screen, there are now 70 variables in the list. Additionally, these variables can be altered by any screen or function within the app. Further violations of “encapsulation” including the general asynchronous execution within Thunkable.

To navigate this landmine of Uber-global variables, I use the following technique:

At the beginning of an event or function, I create a dummy loop through an empty list. Then I “initilize” the variable to some value. Both of these steps are required. After this, you can use MyVariable just like in any other programming language.

The loop acts as a declaration, the set acts as an intilization. You need to be meticulous about the declarations AND intilizations, as Thunkable will crash if you reference an “uninitilized” variable.

Happy Thunking!

non-persistent data variables
function type - scoped to the screen they are created in. leave screen and come back = new value
app type - scoped across screens but only to that that instance of the app running. close app and open again = new value

persistent data
stored type -scoped across screens. data doesn’t change if you close the app and reopen. but if you delete the app and reinstall, goodbye data.
cloud type - scoped across screens. if you data is stored properly, this data will persist against installs/deletes

1 Like