Using Globals

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Using Globals

Post by drhoward »

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?
Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Using Globals

Post by Dixie »

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
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Using Globals

Post by dunbarx »

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
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Using Globals

Post by drhoward »

Thank you! That explains a lot and handled my problem.
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Using Globals

Post by MaxV »

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
Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Using Globals

Post by Mikey »

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.
FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10103
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Using Globals

Post by FourthWorld »

MaxV wrote:The best practice is not to use global variables at all, but custom properties.
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.

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
Mikey
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 755
Joined: Fri Jun 27, 2008 9:00 pm

Re: Using Globals

Post by Mikey »

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.
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10501
Joined: Wed May 06, 2009 2:28 pm

Re: Using Globals

Post by dunbarx »

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
Post Reply