>on 17/11/00 3:20 pm, Adam Wishart at [EMAIL PROTECTED] wrote:
>
>--can anyone help me with Global Variables?????


Normally a variable used in a handler is good only to the end of the 
handler.  It does not keep its value once you leave the handler.  This is 
what is known as "scope".  Once you leave the handler the variable becomes 
"out of scope" and is effectively destroyed.  The next time into the 
handler you essentially start the process over.

Of course, there are many needs to keep variables alive across handlers, 
for the length of the program, even across movies.  Also there is a need to 
retain a value from one handler visit to another.  This is where global 
variables come in.

Once you say you're going to use a variable in "global mode", so to speak, 
it will be entered into the global name pool and will keep its value 
persistent.  Even if you leave the handler, it still keeps its value.  If 
you transfer to another movie (or a movie in a window) you can still get at 
that value.

To declare that you want to use a variable in "global mode", you must 
declare it as global before you go to use it in that scope.  For instance:

on startMovie
   global username

   username ="Bob"
end


If username was a "regular" variable , it would have lost its value ("Bob") 
after you hit the "end" statement.  But because it was declared as global 
it instead is entered into the global name pool and can be referenced 
elsewhere in the system.

To get at the variable elsewhere, you must also declare the name as global 
and then you can reference it.  For instance:

on enterFrame
   global username

   alert "Hello "&username
end

Unlike regular programming languages, declaring here is not really 
allocating space for it, but rather specifying which name pool you will be 
working out of.  Therefore you must do it before the first reference to it 
in a handler.

One shortcut is to list globals at the top of the script, before any 
handlers.  Then, they're valid for the whole script member.  Example:

---- Top of script window ----

global username


on startMovie
   username ="Bob"
end


on enterFrame
   alert "Hello "&username
end

---- End of script window ----

To remember what's global and what's not, many people like the convention 
of putting a little 'g' in front of the name.  This has no technical 
significance, but it can be handy for you and others in reading the 
program.  In this example, username would therefore actually be declared 
and used as gUsername.  Again, it's only for your convenience.  Lingo 
doesn't care what you call it as long as you're consistent.

Two last points:  You can display globals in the message window, which is 
nice.  For instance, having executed the above code, you could do in the 
message window:

put username   (or put gUsername, if you called it that)
-- "Bob"

You can also assign globals in the message window.  Any variables you 
create in the message window are by default global.

username ="Nancy"

There is also a message window command you can use, which is:  showglobals
which dumps all globals to the message window.

Hope this helps.

- Tab

[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to