# Global and local variable

**URL:** <https://forum.kirupa.com/t/global-and-local-variable/11547>\
**Category:** Uncategorized\
**Created:** [January 18, 2003, 10:59pm UTC](https://forum.kirupa.com/t/global-and-local-variable/11547 "2003-01-18T22:59:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rynoe](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@rynoe](https://forum.kirupa.com/u/rynoe)\
**Post date:** [January 18, 2003, 10:59pm UTC](https://forum.kirupa.com/t/global-and-local-variable/11547/1 "2003-01-18T22:59:24Z")

</div>

I can’t figure out how to declare a global variable and call it from another area. Also I have funny things happening with the variables I am using regarding local stuff. Does anybody know if there is a tute on scope and lifetime on variables somewhere?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 18, 2003, 11:31pm UTC](https://forum.kirupa.com/t/global-and-local-variable/11547/2 "2003-01-18T23:31:26Z")

</div>

I wrote something up about that for one of the classes I taught; Ill see if I cant dig it up for you somewhere. But for a quick answer, global variables are defined with the prefix **\_global**. Once you make a global variable, it is accessible from everywhere. For instance, to make the global variable called _sealevel_ you would use:

\_global.sealevel = 100;

Then in Any scope or any movieclip you can reference the variable _sealevel_ and it will be 100 well 100 on 2 conditions 1) you have not changed the global sealevel from 100 to another value 😉 or 2) there isnt a variable already existant within that scope with the same name and a different value. What that means is that if you declare _sealevel_ as a global but then give the movieclip _ocean2_ the variable sealevel and give it a value of something like 0…

ocean2.sealevel = 0

then calling sealevel in the scope of _ocean2_ will result in the value of 0 since THAT sealevel has precedence over the global. Basically what \_global is, is a synonym for Object.prototype. For Flash 5, Object.prototype was the way to make a “global” variable. Its not exactly the same, but its close enough conceptually speaking. All core objects inherit from Object object so adding something to the Object.prototype means that all other objects (including movieclips) would inherit that variable and value - this is of course, again, unless over-ridden.

be careful though, when using a global variable, make sure to use it without any scope defining prefixes. Otherwise, flash will look for the variable of that name IN that scope and not globally. Basically its telling flash to use the over-ridden variable in that scope - but if it doesnt exist, undefined is returned. ex:

\_global.a = “variable a”  
trace(a); // traces “variable a”  
trace(this.a); // traces undefined  
a = “my very own a”  
trace(a); // traces “my very own a”  
trace(this.a); // traces “my very own a”

which ALSO means, if you ever want to change the value of a global variable you HAVE to precede it with \_global or else a single variable within that scope of that name will be created overriding the global and not setting it.

to set

\_global.gVariableName = value;

to reference

myValue = gVariableName;

This might be little help if all the scoping hoopla has still got you down… Ill see if I cant find that write-up I did…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 18, 2003, 11:49pm UTC](https://forum.kirupa.com/t/global-and-local-variable/11547/3 "2003-01-18T23:49:28Z")

</div>

ok let me ask if you got a sec.  
in:

\_global.a = 100;  
this.onEnterFrame= function(){  
b=a+100;  
}

b would be 200

but this:

\_global.a =100;  
this.onEnterFrame= function(){  
a=a+100;  
b=a;  
}

b would be 100.

and in this:

\_global.a =100;  
this.onEnterFrame= function(){  
a=\_global.a  
a=a+100;  
b=a;  
}  
b is 200 and  
there is actually three different variables?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 18, 2003, 11:57pm UTC](https://forum.kirupa.com/t/global-and-local-variable/11547/4 "2003-01-18T23:57:35Z")

</div>

yes, that was what I explained above. If you DO NOT use \_global before the variable when you set it, it doesnt refer to the global, but a standard basic variable specific to that scope

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 19, 2003, 1:01am UTC](https://forum.kirupa.com/t/global-and-local-variable/11547/5 "2003-01-19T01:01:24Z")

</div>

I get it, That is backwards to other languages.
