Page 1 of 1

Global variables

Posted: Fri Dec 12, 2014 6:19 pm
by anmldr
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

Re: Global variables

Posted: Fri Dec 12, 2014 6:43 pm
by Klaus
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

Re: Global variables

Posted: Fri Dec 12, 2014 7:59 pm
by dunbarx
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