Global variables & multiple stacks

Rob Cozens rcozens at pon.net
Mon Apr 14 11:05:01 EDT 2003


Hi Barry,

>What happens if I have identical global variables that I've 
>accidentally used in both (previously separate) stacks? Are global 
>var's truly global in that the data they hold are shared among all 
>sub-stacks?

Once a global variable is declared to the MC engine, ALL stacks (not 
just substacks of the main stack) that reference that global will 
access the same address in RAM, and global values will persist until 
the engine quits.

>Are there variable types that are only "global" to a stack?

"Global" to all handlers in all controls on all cards...no.

However any declared local variable (eg: local myPseudoGlobal) in a 
script is available to all handlers declared after it in the same 
script; so myPseudoGlobal declared as the first line of a stack 
script is now "global" to every handler in the stack script.

Taking it one step further, one can give handlers in other controls 
access to the variable by placing getVariable & setVariable handlers 
in the stack script:

function getMyPseudoVariable
   return myPseudoVariable
end getMyPseudoVariable

on setMyPseudoVariable newValue
   put newValue into myPseudoVariable
end setMyPseudoVariable

myPseudoGlobal is now available to every control in the stack  (and 
beyond, if it's in the script of a stack in use), albeit one must use 
the get & set functions in handlers outside of the stack script.
-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.com/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the Use-livecode mailing list