Variable problem!

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


// 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!

sure =)

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);

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.

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

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

let me try and implement it just a minute thanks :slight_smile:

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

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…

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();

yea :slight_smile: ok im gonna work through it all again, thanks again mumu :wink:

no problem 24.82.95.1 :stuck_out_tongue:

hmm one other unrelated topic while implementing it…

if I set this as a global function


_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

I figured it all out :slight_smile: