This content has been marked as final. Show 6 replies
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.
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.
"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 ..
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 ..
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!
>. 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.
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.