6 Replies Latest reply on Sep 20, 2006 5:00 PM by Newsgroup_User

    _global Scope

    HisHeir Level 1
      Macromedia's documentation implies that once declared _global an Object, function and/or variable is independant of scope,
      and that _global.varname and varname are synonomous.

      That does not seem to be the case ... for example ...

      in root ....
      _global.test = 1


      trace(test + " " + _global.test) yeilds test = 1 and _global.test = 0

      there seems to be 2 variable objects instantiated ..

      Have I missed something ???
        • 1. Re: _global Scope
          Rothrock Level 5
          Defining a _global variable does not prevent a local variable with the same name from being defined. So anytime you assign a value you need the _global. You don't need the _global when referencing, but if there is a local variable with the same name it will obscure the _global one.

          At least that is how I think it works. I've not had problems.
          • 2. Re: _global Scope
            why not just use _global.varname whever referencing the global variable? That would make the code easier to read, and avoid confusion with local variables that may have the same name.
            • 3. Re: _global Scope
              HisHeir Level 1
              "You don't need the _global when referencing" ... that also doesn't seem to be the case. If you attempt to reference the var test above from within a function within a clip the return is 0 ..
              bizarre ...
              the only fix seems to be the ref the var as _global.var ... which seem contrary to the docs and a waste of typing energies ..
              • 4. Re: _global Scope
                Rothrock Level 5
                Okay, "You don't need the _global when referencing AND there is not already a local variable created to obscure the global one."

                But as CogDev pointed out just use _global all the time to avoid confusion.

                PS: Maybe I'm wrong on this and I will test it tonight!
                • 5. Re: _global Scope
                  Level 7
                  >. trace(test + " " + _global.test) yeilds test = 1 and _global.test = 0

                  _global.test would not trace 0... it would trace 1 or it would trace
                  undefined. In your code it would trace 1. As Rothrock stated, if you
                  have globals and locals with the same name then you need to use _global.
                  Makes perfect sense actually. And it is good practice to always use
                  _global when referencing globals.

                  • 6. Re: _global Scope
                    Level 7
                    Very simple

                    If you are simply referencing a _global variable, and there is not other
                    local or timeline variable of the same name, you do not need the "_global."

                    If you are changing it (including '++' operator), or if there is a local or
                    timeline variable of the same name, then you DO need the "_global." prefix.

                    HOWEVER .. it is good practice to ALWAYS include the "_global." prefix as it
                    ensures you will always get the global variable and because it is good
                    coding style as you can then tell that a particular variable is global (and
                    globals should be used sparingly and only with good reason).

                    NOTE: A general exception to this is when you have define a _global
                    function. One tends not to then use the "_global." prefix (unless it is
                    strictly necessary). But for variables, it is best to use "_global." all
                    the time to avoid problems and confusion.