Using Globals
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Using Globals
I'm having a problem using global variables. In the main stack (I only have 1 stack). I'm declaring a variable to be global as follows: Global gAbortFlag
Then in the handler openStack I'm setting it to false using the following code: put false into gAbortFlag
I have an abort button defined and when I click on this button, it sets gAbortFlag to true using the following code: put true into gAbortFlag
In some long running loops in the program gAbortFlag is then checked to see if the loop should keep running. The abort button is designed to completely terminate the run.
Unfortunately this doesn't work. So I put the following code into my mouseUP routine for my abort button: answer gAbortFlag
When I push the button, this brings up a message box that contains "gAbortFlag", so I assume that it does not recognized the global variable.
I'm using LiveCode version 7.0.3.
What am I not understanding about declaring and using global variables?
Then in the handler openStack I'm setting it to false using the following code: put false into gAbortFlag
I have an abort button defined and when I click on this button, it sets gAbortFlag to true using the following code: put true into gAbortFlag
In some long running loops in the program gAbortFlag is then checked to see if the loop should keep running. The abort button is designed to completely terminate the run.
Unfortunately this doesn't work. So I put the following code into my mouseUP routine for my abort button: answer gAbortFlag
When I push the button, this brings up a message box that contains "gAbortFlag", so I assume that it does not recognized the global variable.
I'm using LiveCode version 7.0.3.
What am I not understanding about declaring and using global variables?
Re: Using Globals
Have you declared the global in your button script ? It needs to be declared in the script of each object that will use its value... something like
Code: Select all
global onAbortFlag
on mouseUp
put false into gAbortFlag
end mouseUp
Re: Using Globals
Hi.
What Dixie said.
Declaring a global in a script is fine, but that does not automatically make it available system-wide. It must be declared in every script that it is required. Note also that the script declaration must be made ABOVE any handlers that may need it. Common practice, therefore, is to declare it at the top of the script.
This is in contrast to custom properties, which are in fact more ubiquitous and accessible. Once set, they may be accessed at any time from any place; script, message box, whatever. If accessed from outside the stack. you do have to make sure that pathway references are valid and correct.
Craig Newman
What Dixie said.
Declaring a global in a script is fine, but that does not automatically make it available system-wide. It must be declared in every script that it is required. Note also that the script declaration must be made ABOVE any handlers that may need it. Common practice, therefore, is to declare it at the top of the script.
This is in contrast to custom properties, which are in fact more ubiquitous and accessible. Once set, they may be accessed at any time from any place; script, message box, whatever. If accessed from outside the stack. you do have to make sure that pathway references are valid and correct.
Craig Newman
Re: Using Globals
Thank you! That explains a lot and handled my problem.
Re: Using Globals
The best practice is not to use global variables at all, but custom properties.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Using Globals
Here are a couple of other tips on scoping variables that you might not have run into, yet:
1) If you define a variable as global inside of a script, but outside of a handler (remember, in LC, a script is essentially a window of code, and a handler is the code for handling an event like mouseUp or openCard), then that declaration applies to every handler in that script (window)
2) If you want a variable to be available to every handler in a script, but nowhere else (perhaps on this card only), you can declare it as being local instead of global, and the same rules will apply.
1) If you define a variable as global inside of a script, but outside of a handler (remember, in LC, a script is essentially a window of code, and a handler is the code for handling an event like mouseUp or openCard), then that declaration applies to every handler in that script (window)
2) If you want a variable to be available to every handler in a script, but nowhere else (perhaps on this card only), you can declare it as being local instead of global, and the same rules will apply.
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Using Globals
Binding data to specific physical objects can be useful when the data relates to those objects, or when persistence is needed and stack files are used as the data storage format.MaxV wrote:The best practice is not to use global variables at all, but custom properties.
But most programming languages support globals, and for good reasons. They're simple to work with, more efficient, and cannot be inadvertently saved across sessions for session-specific data.
Both custom properties and global variables are very valuable, but given how very different they are in terms of their ideal use cases I would hesitate to suggest that one be completely avoided in favor of the other. Each has their place.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Using Globals
The point about persistence is well-taken, and "ease of use" is one of the reasons why I like LC - dynamic typing is soooooooooo great.
Re: Using Globals
Just a note with regard to what Richard said. Globals are much faster, perhaps ten times faster, than custom properties. This is surely an important consideration, and I love custom properties, using them wherever I can.
Craig
Craig