Author Topic: Strange but with getDefinitionByName.  (Read 1887 times)

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Strange but with getDefinitionByName.
« on: Mon, Aug 20, 2012 »
Hi,

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?
blog, twitter, Check out my award winning game, Rot Gut:

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Strange but with getDefinitionByName.
« Reply #1 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.
blog, twitter, Check out my award winning game, Rot Gut:

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
Re: Strange but with getDefinitionByName.
« Reply #2 on: Mon, Aug 20, 2012 »
For me, any class that isn't in the root directory requires the full qualified path. For example:

getDefinitionByName("Player")

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

getDefinitionByName("net.axgl.starstep.Player")

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.

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Strange but with getDefinitionByName.
« Reply #3 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:

getDefinitionByName("src/EnemyPatrolTommyGun")

?
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.
« Last Edit: Mon, Aug 20, 2012 by test84 »
blog, twitter, Check out my award winning game, Rot Gut:

Denis

  • Member
  • **
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Strange but with getDefinitionByName.
« Reply #4 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:
http://board.flashkit.com/board/archive/index.php/t-733209.html
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;
or
if(tmpVar is MyClass) dumb();

As for the name string you should use, try
getQualifiedClassName(myClassObj);
you will see something like:
"Level1Folder.Level2folder::MyClass"

"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.

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Re: Strange but with getDefinitionByName.
« Reply #5 on: Fri, Aug 31, 2012 »
Check out this link:

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.