Beginners global variable pb
Ken Ray
kray at sonsothunder.com
Thu Feb 19 15:50:25 EST 2004
> Since I defined the variable as global up in the stack I
> don't understand why it doesn't apply to every card and
> field. Maybe there is another way to do this?
No, you have it right, but there's a second step that needs to be taken.
Once a global variable is defined and given a value, other scripts that
need to retrieve the value from the global need to make that global
accessible. This is done through re-declaring the global in the script
that wants to retrieve the value. Note also that the "global"
declaration can be either *inside* a handler, or *outside* a handler. If
it's *inside*, the access to the global is only for *that* handler (and
no others in the script, unless they too have declared the global in
their handlers). If it's *outside", it is accessible by all handlers in
that script. Note that if you try to use a global to which you don't
have access, Rev will think you're talking about an unquoted string and
will return the name of the variable instead (see the examples below).
For example, if in your stack script you had:
on openStack
global gMyGlobal
put "Hello" into gMyGlobal
end openStack
and then on some button on some card you wanted to get (or change) the
global gMyGlobal, you could do it in one of two ways (note I'm showing
two handlers in a button script to describe the difference between
declaring access to the global inside or outside of a handler):
1) Inside the handler:
on mouseUp
global gMyGlobal
put gMyGlobal --> puts "Hello" into the message box
DoMyCustomHandler
end mouseUp
on DoMyCustomHandler
answer gMyGlobal --> brings up an answer dialog with the word
"gMyGlobal" in it
-- This is because inside this handler, gMyGlobal is an unknown
variable and
-- Rev treats it as an unquoted string
end DoMyCustomHandler
2) Outside the handler:
global gMyGlobal
on mouseUp
put gMyGlobal --> puts "Hello" into the message box
DoMyCustomHandler
end mouseUp
on DoMyCustomHandler
answer gMyGlobal --> brings up an answer dialog with the word "Hello"
in it
end DoMyCustomHandler
I hope this is clear; if not, please let me know and I (or someone else
on this list) will clarify.
> P.S. Oh! Just one more thing, is there a way to have an image
> put in such away that it appears in every cards at the same
> place when you create a new card? And if you modify that
> image you don't have to copy and paste hundreds of times? It
> ain't a priority but it would be cool ;-)
Yes. What you do is put the image into a group (select the image and
choose "Group Selected" from the Object menu). Then set the
"backgroundBehavior" property of the group ("Behave like a background")
to "true" (check the checkbox in the Properties palette for the group).
If this is done on the last card of your stack, any subsequently created
cards will have that image on it. To apply it to cards you already have
created, go to a card that doesn't have it and choose "Place Group" from
the Object menu.
Hope this helps,
Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/
More information about the use-livecode
mailing list