# Variable problem!

**URL:** <https://forum.kirupa.com/t/variable-problem/185718>\
**Category:** flash\
**Created:** [April 15, 2006, 8:25pm UTC](https://forum.kirupa.com/t/variable-problem/185718 "2006-04-15T20:25:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 8:25pm UTC](https://forum.kirupa.com/t/variable-problem/185718/1 "2006-04-15T20:25:25Z")

</div>

Here’s what I need to do  
I need to feed the path of a target to a function and it needs to rewrite to set a value to it.

example below

```auto

// Does Not Work
targetPath = "path1.box1";
_root[targetPath]._alpha = 50;
trace(_root[targetPath]._alpha);
// Traces undefined

// Does Work
targetPath = "box1";
_root[targetPath]._alpha = 50;
trace(_root[targetPath]._alpha);
// Traces 50

```

Basically it works if it’s only one target name with no previous/subsequent targets like

path1 works  
but not  
path1.box

is there any way to fix this?

thanks!

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 15, 2006, 9:09pm UTC](https://forum.kirupa.com/t/variable-problem/185718/2 "2006-04-15T21:09:38Z")

</div>

sure =)

```auto
this.object1 = new Object();
this.object1.object2 = new Object();
this.object1.object2.object3 = new Object();
this.object1.object2.object3.someVar = "xxx";

function getReference(path:String):Object {
	var broken:Array = path.split(".");
	reference = this[broken[0]];
	for (var i = 1; i<broken.length; i++) {
		reference = reference[broken*];
	}
	return reference;
}

var path:String = "object1.object2.object3";
trace(getReference(path).someVar);

```

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 9:16pm UTC](https://forum.kirupa.com/t/variable-problem/185718/3 "2006-04-15T21:16:05Z")

</div>

hmm any less clutterfull way? I have to run this through a universal function and thats alot of code to add / clutter? I might end up jus tmake the input be an array and then have the path put it together.

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 15, 2006, 9:18pm UTC](https://forum.kirupa.com/t/variable-problem/185718/4 "2006-04-15T21:18:52Z")

</div>

What? That function has 5 lines if I skip its declaration and all the brackets… everything around is just to show off its use…

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 9:23pm UTC](https://forum.kirupa.com/t/variable-problem/185718/5 "2006-04-15T21:23:26Z")

</div>

ohh haha the extra lines scared me from even looking at how it works 🙂

let me try and implement it just a minute thanks 🙂

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 9:32pm UTC](https://forum.kirupa.com/t/variable-problem/185718/6 "2006-04-15T21:32:46Z")

</div>

so hmm I’m trying to figure out how the whole thing works cause I know it works and I got it to work with I need it to but I’d prefer to understand it so I can use it later :S

majorly the massive use of : between a variable and whatever ive never explored it in flash

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 15, 2006, 9:44pm UTC](https://forum.kirupa.com/t/variable-problem/185718/7 "2006-04-15T21:44:22Z")

</div>

Thats data typing… it just specifies data type of a variable (Number, String, MovieClip, BitmapData or any other class and interface) or a return type of a function (in this case its Object).

I have one little modifaction for that function…

```auto
function getReference(path:String, scope:Object):Object {
    var broken:Array = path.split(".");
    reference = scope[broken[0]];
    for (var i = 1; i<broken.length; i++) {
        reference = reference[broken*];
    }
    return reference;
}

```

Till now it allways looked for desired path in the scope, where function was. Now it doesnt realy matter where that function is declared and you can use it with specific scope… getReference(“someObj.someOtherOne”, \_root) will look for someObj.someOtherOne in \_root or getReference(“someObj.someOtherOne”, this) will return a reference to someObj.someOtherOne from where you are at the moment…

//edit one last thing to mention… Im sure you already know it but anyway…  
Because that function returns the actual reference, you can store it and use it as normaly… for example:  
someMovieClip = getReference(“someObj.someOtherOne.mc1”, this);  
someMovieClip.play();

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 9:58pm UTC](https://forum.kirupa.com/t/variable-problem/185718/8 "2006-04-15T21:58:05Z")

</div>

yea 🙂 ok im gonna work through it all again, thanks again mumu 😉

---

<div class="post-metadata">

**Author:** ![mathew\_er](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mathew\_er](https://forum.kirupa.com/u/mathew_er)\
**Post date:** [April 15, 2006, 10:04pm UTC](https://forum.kirupa.com/t/variable-problem/185718/9 "2006-04-15T22:04:25Z")

</div>

no problem 24.82.95.1 😛

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 10:30pm UTC](https://forum.kirupa.com/t/variable-problem/185718/10 "2006-04-15T22:30:06Z")

</div>

hmm one other unrelated topic while implementing it…

if I set this as a global function

```auto

_global.getTarget = function(path:String, scope:Object):Object {
    var broken:Array = path.split(".");
    target = scope[broken[0]];
    for (var i = 1; i<broken.length; i++) {
        target = target[broken*];
    }
    return target;
    trace(target);
};

// this would be inside a different function
getTarget(path, this)._alpha = 50;

should work no? It's not even running the getTarget function cause it's not tracing :S

```

it should work right? It’s giving me troubles :S

---

<div class="post-metadata">

**Author:** ![Flaevo](https://avatars.discourse-cdn.com/v4/letter/f/bbce88/32.png) [@Flaevo](https://forum.kirupa.com/u/Flaevo)\
**Post date:** [April 15, 2006, 11:07pm UTC](https://forum.kirupa.com/t/variable-problem/185718/11 "2006-04-15T23:07:39Z")

</div>

I figured it all out 🙂
