# Accessing outside variables from functions

**URL:** <https://forum.kirupa.com/t/accessing-outside-variables-from-functions/59514>\
**Category:** flash\
**Created:** [August 1, 2004, 4:04pm UTC](https://forum.kirupa.com/t/accessing-outside-variables-from-functions/59514 "2004-08-01T16:04:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flash\_monkey1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flash\_monkey1](https://forum.kirupa.com/u/flash_monkey1)\
**Post date:** [August 1, 2004, 4:04pm UTC](https://forum.kirupa.com/t/accessing-outside-variables-from-functions/59514/1 "2004-08-01T16:04:29Z")

</div>

I can’t figure out how to access variables that are declared in a movie clip from a function declared in that same movie clip.

i.e.

* * *

var int1 = 1  
var int2 = 2

function myfunction(){  
int1++;  
int2++;  
}  
myfunction();

* * *

I’m trying to do this in the main movie clip of a movie that I plan on importing dynamically in another movie. I’ve tried to use this.int1++, but I think that flash was looking for a variable that belongs to “myfunction” when I did that. I could just use \_root.int1++ but like I said, I plan on dynamically loading this into another movie, in which case it would be pointing to the wrong movie.

Help!!

---

<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:** [August 1, 2004, 8:22pm UTC](https://forum.kirupa.com/t/accessing-outside-variables-from-functions/59514/2 "2004-08-01T20:22:53Z")

</div>

> [@flash\_monkey](#):
>
> I can’t figure out how to access variables that are declared in a movie clip from a function declared in that same movie clip.
> 
> i.e.
> 
> * * *
> 
> var int1 = 1  
> var int2 = 2
> 
> function myfunction(){  
> int1++;  
> int2++;  
> }  
> myfunction();
> 
> * * *
> 
> I’m trying to do this in the main movie clip of a movie that I plan on importing dynamically in another movie. I’ve tried to use this.int1++, but I think that flash was looking for a variable that belongs to “myfunction” when I did that. I could just use \_root.int1++ but like I said, I plan on dynamically loading this into another movie, in which case it would be pointing to the wrong movie.
> 
> Help!!

if i`m understanding this correctly, what you have written above will work as it is. You could declare the variables like  
this.int1 =1

---

<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:** [August 2, 2004, 12:03am UTC](https://forum.kirupa.com/t/accessing-outside-variables-from-functions/59514/3 "2004-08-02T00:03:05Z")

</div>

that is correct. those variables are declared in the scope of that script - the timeline script in whatever movieclip (beit \_root or anyother) in which it was written. As such, it belongs to and is defined in that clip. myfunction being defined in that script actually has double access to those variables. 1) because it is defined in the same _script_ as the variables and 2) because its defined in the same _scope_ of those variables, that being the timeline. access via 1 is simply through the variables names as used above. access via 2 is through this.variablename. Both should work given the circumstance above.
