Scrolling a Card?
J. Landman Gay
jacque at hyperactivesw.com
Fri Feb 20 21:09:37 EST 2004
On 2/20/04 5:53 PM, Christopher Mitchell wrote:
> Could you please explain under what circumstances this handler would
> require to be explicitly declared? This seems like something implicit
> to the engine, and if not handled reliably then how many other things
> need to be re-defined?
That's not exactly how it works; it isn't about defining but rather
about access. Globals exist in memory until Rev quits, or until you
issue a command to explicitly delete them. A variable is "global"
because it is persistent for the entire current session. But to actually
use it, it must be explicitly declared in any code that wants to access
it. For convenience, you can define the scope of the global by simply
placing the declaration at the right level in the script.
The most basic level is the handler. If you only have one or two
handlers that need to use that global, then it makes sense to declare
the global only in those handlers. The global variable will be unknown
and invisible to any other handler, and if you happen to use a
same-named variable within those other handlers, the variable will be
treated like a local. That's because those other handlers don't know the
global exists.
But the two handlers that do declare the global will both be able to
"see" it:
on summIt
global num1,num2 -- assume these already have values
put sum(num1,num2) -- put the sum into the msg box
end summIt
on subtractIt
global num1, num2
put num1 - num2
end subtractIt
If, however, you have a whole bunch of handlers that need to access a
global, and those handlers are all in the same script, then you can move
the placement up a level and declare the global outside of any handler.
That makes it available to any handler in the script. Usually these
script-wide globals are declared at the top of the script:
global num1,num2
on summIt
put sum(num1,num2)
end summIt
on subtractIt
put num1 - num2
end subtractIt
on divIt
put num1/num2
end summIt
on multiplyIt
put num1 * num2
end subtractIt
There is no way to declare the scope of a global stack-wide. You'll have
to declare it in every script that uses it.
You can create globals, fill them with values, see them in the variable
watcher, and let them take up memory, but if no handler or script ever
declares them, then they will remain invisible to your code.
--
Jacqueline Landman Gay | jacque at hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
More information about the use-livecode
mailing list