Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pro and cons of global variables

39 views
Skip to first unread message

webmaster

unread,
Apr 18, 2002, 11:25:59 AM4/18/02
to
Hi,

What are the pros and cons of global vars ?
Is is good to have a lot of global variables in a cdrom application ?
Is it better to use sprite scripts properties when possible ?

Any help and URL about this kind of subject will be appreciated.

thanx.

Leo Prendergast

unread,
Apr 18, 2002, 12:46:00 PM4/18/02
to
Global variables are largely unavoidable when working on a project of
reasonable size and complexity. It's generally agreed that too many globals
(AKA "Global Soup") leads to problems such as naming conflicts and is
counter to an OOP way of working - where data always belongs to an object,
rather than being accessible to all and having the potential to be set from
anywhere (to the extent that it becomes close to impossible to track which
changes were made, when and by which object/script). For these reasons (and
I'm sure many more) it's wise to limit the use of globals where possible.

Some suggest never using them which I would consider to be taking things too
far. Elaborate systems built to do away with the need for globals add
unnecessary complexity to projects and Director is largely about rapid
development rather than creating conceptually pleasing code.

I would suggest that wherever a property variable can be used, use it. If
you must use a global, use it but do think whether it is really necessary.

I'm sure that some will dissagree.

--
Leo Prendergast
Interactive Designer for hire
Director - Lingo - Flash - ActionScript - Dreamweaver
leo...@yahoo.com
--
webmaster <webm...@arpp.fr> wrote in message
news:a9moc2$ips$1...@forums.macromedia.com...

rich

unread,
Apr 18, 2002, 1:08:24 PM4/18/02
to
Is it true that globals are stored in the actual computers memory and stay
there until cleared?

"Leo Prendergast" <leo...@yahoo.com> wrote in message
news:a9mt8f$t2l$1...@forums.macromedia.com...

Leo Prendergast

unread,
Apr 18, 2002, 1:38:14 PM4/18/02
to
Yes, this is also true Rich. Even if you set a global to VOID or use
clearGlobals, the variable is still there (empty and for all intents and
purposes just the same as an uninitialised variable) and it's name can be
retrieved using "the globals.getPropAt()" (showGlobals will not detect it).
This will be true until the player engine quits (lasting even beyond the
scope of the current movie - in a projector or shockwave).

I don't know at what point you would run into trouble from this and I'm no
engineer so I don't really know what the ramifications are in terms of
performance. If you are careful about your use of globals then you are never
likely to have any issues with this. So, all things in moderation.

For what it's worth, global lists can often be successfully used to
streamline the use of globals, keeping their number down, them neatly
arranged and protecting their namespace to a degree. Clearing a global list
or voiding it leaves only one footprint rather than many, since it becomes
an empty variable rather than a list. Again, no idea whether this would be
useful or not for the sake of performance.

--
Leo Prendergast
Interactive Designer for hire
Director - Lingo - Flash - ActionScript - Dreamweaver
leo...@yahoo.com
--

rich <rbar...@yahoo.com> wrote in message
news:a9mudv$27k$1...@forums.macromedia.com...

Luke

unread,
Apr 18, 2002, 8:43:56 PM4/18/02
to
webmaster wrote:

> Hi,
>
> What are the pros and cons of global vars ?

The advantage and disadvantage of globals - as compared to 'properties' -
relates to their different 'scope'. Globals can be read and changed by any
handler anywhere, whereas properties can only be accessed either by the
object that 'owns' the properties, or by specifying which object has the
property.

The advantages of globals are:

- easy way to store and share data
- easy way to maintain variables across movies

Disadvantages of globals are:
- no scope protection, which means...
- a sloppy way to store and share data
- a sloppy way to maintain data across movies

The reasons why scope protection and encapsulating data in objects is
important and useful is perhaps best appreciated as part of a broader
'object orientated' programming/design philosophy (OOP). In a nutshell,
OOP is about sending messages to objects. An object has both data (which are
its 'properties') and 'methods' which act on that data. To an object
orientated programmer, data belongs to an object and hence the data should
only be changed by the object that "owns" it. This is achieved by sending
messages to the owner object (In Director, strict scope protection is not
possible since you can change an objects data directly if you wanted to).
The methods or handlers that an object uses to respond to messages are often
called the object's interface. This interface encapsulates an object's data
and methods of manipulating that data, and allows you to separate an
object's behaviour from its implementation (Xtras are a good example of this
- you communicate with an Xtra without having to know how the Xtra was
implemented).

> Is is good to have a lot of global variables in a cdrom application ?

As a general rule, try not using any globals (except perhaps the 'global
containers' - such as the actorList - Director provides). If you try to
avoid using them, it helps you think about data structures, object
encapsulation etc.

There are cases where globals can be useful though (for example, in a game -
it can make things quicker keeping frequently read data stored in a global
rather than using function calls or ancestor relationships to share that
data amongst various objects)

Luke


----------------------------------------------------------------
| Mecca Medialight | http://www.meccamedialight.com.au/ |
| Lingo Resources | http://www.lingoworkshop.com/ |
----------------------------------------------------------------

Kevin Burtt

unread,
Apr 19, 2002, 12:38:03 PM4/19/02
to
Programming and OO "purists" say global variables should be avoided (and in
fact were outlawed completely in some of
my university classes)

However since Director is designed for use by non-coders as well as
hard-core programmer types, the "end justifies the means"
theory applies (also, the "whatever works" theory) and global variables can
be used effectively with a minimun of risk.

Some issues:

--as mentioned above, global variables stay resident in memory even between
multiple movies. Therefore, make sure that all
movies that are or can be run at the same time (through "go to" references
or MIAWs, etc...) don't have two different variables
that have the same name, as the second instance will overwrite the first
one. It's okay if it's the SAME variable that will be used between
multiple movies as long as the movies are designed that way. Using a two or
three letter header before global variable names in each movie will work
towards avoiding name conflicts.

--I think it's a pain to put the "global <name>" headers in every script
file, plus removing all of them from memory at the end requires a lot of
work. I recommend
the global list method mentioned by Leo in his post above.
Example: global GlobalData
(initialized in prepareFrame or somewhere)
GlobalData = [#myGlobalOne: 6, #myGlobalTwo: "Hello World"]

referenced as: localVar = GlobalData.myGlobalOne + 16

This helps in readability and in keeping all globals in one place (as well
as just putting one 'global GlobalData' header in each script)

I haven't done any research as far as the efficiency (program speed) of
global variables, but this technique has been very worthwhile for me in my
(brief) time as Director developer.

Kevin Burtt

"There are two groups of people in the world:
Those that like to divide the people of the world into two groups,
and those that don't."

"webmaster" <webm...@arpp.fr> wrote in message
news:a9moc2$ips$1...@forums.macromedia.com...

Andrew Morton

unread,
Apr 22, 2002, 9:05:35 AM4/22/02
to
> --I think it's a pain to put the "global <name>" headers in every script
> file,

Agreed. Too easy to forget to add a new one in all over the place.

> plus removing all of them from memory at the end requires a lot of
> work.

clearGlobals command?

Andrew Morton


0 new messages