Frequently Asked Question
In order to use the same variable in several widgets, you have to define that variable as global. In that way, you will be able to access that variable and use it in different widgets and functions. In widgets that are not chaining global variable you don't need to use the global keyword. It is important to emphasize that local variable defied in one widget handler will be visible in all handlers within a single widget ("On Click", "On Timer", "On Stop").
In case you define some local variable in the local namespace of some Sub-Panel, which has the same name as the global variable in Panel initialization, only your local variable will be visible in that Sub-panel.
To specify global constants, variables, and functions that will be available in all Macro and Expression scripts located in a Panel, you should specify them in the Panel initialization script. Variables defined as global are visible in all widgets and sub-panels and you have to call them within the widget that needs to use that variable. They can be used in several widgets and sub-panels with different functions. For example in some widgets you can change the value of the variable, in another, you can display the initial value that is defined within the Initialization script, etc.
Here is a short example of how it works:
- First, in Panel Initialization define the property variable
2. Next, there is a button that increases this variable
3. There is the displayed value widget that displays the current value of the variable
So this example demonstrated how you can initialize a global variable within the Panel Initialization script and access that variable from different widgets. Since in this example the initial value of the variable is zero, that means that the display value will display zero until the button is pressed.
If you have a local variable defined in sub-panel initialization that has the same name as the global variable in the Initialization script, only your local variable will be visible in the sub-panel, until you write global before that variable.
Restrictions
In case you want to call a function inside another (nested functions), there is a certain restrictions. To find out more about it, check this FAQ.