00:00
00:00
Newgrounds Background Image Theme

GodsPurpose just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

String to instance name?

383 Views | 6 Replies
New Topic Respond to this Topic

String to instance name? 2015-03-09 01:15:01


Having trouble following what's being said through Google, so I'll ask directly with more simplified examples.

I have vars in the class variables:

var obj1:MovieClip = new MovieClip();
var selectedObj:MovieClip;

I also have a string elsewhere:

var word:String = "obj1";

When I try to convert the string to an object instance name with this method:

selectedObj = this[word];

I get the following error:

ReferenceError: Error #1069: Property obj1 not found on MainClass and there is no default value.

What's the deal, can a class not recognize a converted string as an instance name this way? It works if I just assign the selectedObj as obj1, but I want to be able to cut down on the hard coding.

Any advice is welcome. I'm very tired right now.

Response to String to instance name? 2015-03-09 08:06:14


Can you post all of the relevant code (not just tiny tidibits)?

In the small bit of code you provided, there's nothing that could be causing that error, assuming that those variable definitions and the line selectedObj = this[word]; are all in the same class, as you've not provided access modifiers for the two variables (i.e. public/protected/private) so if you're accessing them through an extended class, they could have been defaulted to private, which would make them inaccessible.

Having said that, there is nothing wrong with accessing a variable that way; so long as the scope you're executing from has access/permission to the object.

Response to String to instance name? 2015-03-09 09:22:37


what might've happened is the code mistook

this[word];

as an array instance

see if

this[word + ""]

does anything

Response to String to instance name? 2015-03-09 09:37:21


At 3/9/15 09:22 AM, ShadowWhoWalks wrote: what might've happened is the code mistook

this[word];

as an array instance

That wouldn't be possible because he's using the this keyword. But even if it were interpreting it as an array, you could still do that because an array is just an object and all objects can have their properties accessed in that manner.

Response to String to instance name? 2015-03-09 09:45:41


At 3/9/15 09:22 AM, ShadowWhoWalks wrote: what might've happened is the code mistook
this[word];
as an array instance
see if
this[word + ""]
does anything

word already is a string, appending an empty string will not change anything.
And i think the type of the index does not matter in as3 because it is always converted to string before using it as a key.
I'm not sure about that though, I have no reference at hand and currently can't test it.

Response to String to instance name? 2015-03-09 11:38:54


At 3/9/15 08:06 AM, Diki wrote: Can you post all of the relevant code (not just tiny tidibits)?

Sorry, the word variable is created in another class, then it's passed to the document class, then to a different class that actually uses it. The characterSelect variable in the document class is public static, if that makes a difference.

Does it also make a difference if word is made up of four different strings "+" together? The situation is that I'm making a character select system that uses an array of letters and an array of four lettered code strings and checks to see if they match. It loops through the letterArray and checks in groups of four if the group matches to any of the codes in the codeArray. If it does, it splices the letterArray and sets the DocClass.characterSelect to equal the group of strings.

Now that I think about it, though, I've set it as the code instead of the group of strings instead and I still get the same error, so that must not be it.

The obj1:MovieClip is also public static at the moment.

Response to String to instance name? 2015-03-09 14:07:56


At 3/9/15 11:38 AM, Barzona wrote: Sorry, the word variable is created in another class, then it's passed to the document class, then to a different class that actually uses it. The characterSelect variable in the document class is public static, if that makes a difference.

Could you post the code that does all that?

And just for the sake of clarity: the obj1:MovieClip object exists in the same class that you have the code selectedObj = this[word];, right? If not: that's the problem.

At 3/9/15 11:38 AM, Barzona wrote: Does it also make a difference if word is made up of four different strings "+" together?

No. Regardless of how the string is formed, it looks the same to the compiler and run-time environment.