Global variables

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
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

Global variables

Post by anmldr » Fri Dec 12, 2014 6:19 pm

If you declare a global variable in a stack script, do you have to declare it again in say a card script or button script that is going to use the variable?
i.e.
global myVar
is in the stack script
Do I have to repeat
global myVar
if I am going to use myVar in a button or card script?

Linda

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Global variables

Post by Klaus » Fri Dec 12, 2014 6:43 pm

Hi Linda,

yes, a global vairable needs to be declared in every SCRIPT, like in these examples:
1. Stack or Card script, does not make much sense, but should show you how it works :D
Global is declared a the top of the script and thus available to all handlers in the script

Code: Select all

global gYourVar

on opencard # openstack
  put 10 into gYourVar
end opencard

command my_handler
  add 20 to gYourVar
  answer gYourVar
end my_handler
2. Button or any other object script, only one handler, so this can be done INSIDE of the handler:

Code: Select all

on mouseup
  global gYourVar
  put gYourVar into fld "display my global"
end mouseup
Hope this helps!


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Global variables

Post by dunbarx » Fri Dec 12, 2014 7:59 pm

What Klaus said.

It may not be pertinent in your situation, especially if the value you want to access across scripts is small, but do consider using custom properties. These are just as ubiquitous, can be of far greater richness in terms of what you are actually storing, and survive sessions. They are like a "super global".

Craig Newman

Post Reply