Flixel Forums

development => help => Topic started by: test84 on Mon, Aug 20, 2012

Title: Strange but with getDefinitionByName.
Post by: test84 on Mon, Aug 20, 2012

I have a strange bug. I'm using getDefinitionByName in order to dynamically create instances from names of my classes but there is one specific class that apparently has no difference from other ones, like it's in the same directory, same namespace, etc but suddenly it's giving "ReferenceError: Error #1065" error message.

This is the line:
Code: [Select]
var EnemyTypeToSpawn:Class = Class(getDefinitionByName(_currentBatchOfSpawnerConfig[i].Type));
which will be this in runtime:
Code: [Select]
var EnemyTypeToSpawn:Class = Class(getDefinitionByName("EnemyPatrolTommyGun"));
and gives the error. I managed to get it work by declaring a variable just before this:

Code: [Select]
var temp:EnemyPatrolTommyGun;
var EnemyTypeToSpawn:Class = Class(getDefinitionByName(_currentBatchOfSpawnerConfig[i].Type));

and it works.

What's the problem?
Title: Re: Strange but with getDefinitionByName.
Post by: test84 on Mon, Aug 20, 2012
To shed more light on the problem, I can manually give class name to that getDefinitionByName and some of class name's work and some don't!

My publish settings is to FP 10 and I tried FP 11, no luck.
Title: Re: Strange but with getDefinitionByName.
Post by: Arkeus on Mon, Aug 20, 2012
For me, any class that isn't in the root directory requires the full qualified path. For example:


Will throw me an error (even if it's in the same directory as the file I'm in), and I have to do:


This wouldn't explain the behavior of adding a variable and having it work though. I thought that was maybe because you needed to import it at the top (and when you added that variable your IDE imported it which caused it to work), but for me even with importing I still need the full path.
Title: Re: Strange but with getDefinitionByName.
Post by: test84 on Mon, Aug 20, 2012
Thanks for the reply. All of source codes are in a src folder inside a directory that has my as3proj file, so do I have to call:


If yes, I tried that and didn't work.

I read your last paragraph 4 times but didn't understand it, sorry.

I semi-solved it by adding a temp array and instantiating all classes inside it, I hate it but that's best solution I found after hours of googling so far and am after a better solution.

Interesting thing is it was working fine until yesterday and I didn't change anything in these files that do not work with getDefintiionByName. Perhaps I changed something else that I do not reference these classes and Flash doesn't include them in the final SWF.
Title: Re: Strange but with getDefinitionByName.
Post by: Denis on Fri, Aug 24, 2012
It's a compiled/not compiled thing. The class which you try to get by getDefinitionByName is not being compiled.
Check out this post:
Just importing the class won't make it work, so you need a reference to it to get it compiled. You can either
var tmpVar:MyClass;
if(tmpVar is MyClass) dumb();

As for the name string you should use, try
you will see something like:

"src/EnemyPatrolTommyGun" won't work.

test84, the interesting thing about the code which was working yesterday is usually that at some point in your project you did have a specific reference to that class. It doesn't matter where it is, until it's within a compiled class. And if you cleared the reference for some reason - that's why it got broken.

A better solution than defining array with variables with class references, would be to get all the needed classes to always compile without making references to them. I guess that could be done trough using ant. Never tried it tough and I'm not sure if it would work, but I plan to check that way.
Title: Re: Strange but with getDefinitionByName.
Post by: LaughingLeader on Fri, Aug 31, 2012
Check out this link:

http://www.ryanwelcher.com/blog/getdefinitionbyname (http://www.ryanwelcher.com/blog/getdefinitionbyname)

I ran into the same problem myself when trying to instance classes dynamically. Simply declaring the class somewhere before you run your getDefinitionByName code will make it work, since Flash then has the definition of the class you're trying to dynamically instance.

You don't have to declare a variable. If your class is called "Player", for instance, just declaring Player will create a definition of it.

I create my level list by dynamically passing in classes from an XML file, but the getDefinitionByName won't pick up the level classes if I don't declare it somewhere first. So I created a "classDefinitions()) function that runs once when my game starts up, and I put all the class definitions I need in there.