Game Development Community

Are Parent Class / Super Class objects allowed in Torque 2D?

by Jeff Moretti · in Torque 2D Beginner · 04/16/2014 (4:15 pm) · 5 replies

Hey guys,

I'm porting over my code to Torque 2D (MIT) and there seems to be a problem when I come across parent classes.

I have the following code:

function basicEnemyInit()
{
   %newBasicEnemy = new Sprite()
   {
      class = "basicEnemy";
      superclass = "enemy";
   };
   %newBasicEnemy.SceneLayer = 3;
   %newBasicEnemy.Image = "Station:BasicEnemy";
   $gameLevel.add(%newBasicEnemy);

   %newBasicEnemy.init();
   
...

   return %newBasicEnemy;
}

Is this the proper way to do a superclass in Torque 2D? If not, can you provide an example on how to do this?

Here's the error I get when I try to do this:

Begin Level 6
Namespace::unlinkClass - cannot unlink namespace parent linkage for BasicEnemy for Sprite.
modules/Station/1/scripts/enemy/basicEnemy.cs (22): Unknown command setUseMouseEvents.
Object (1430) BasicEnemy -> enemy -> Sprite -> SpriteBase -> SceneObject -> BehaviorComponent -> DynamicConsoleMethodComponent -> SimComponent -> SimObject
Error: cannot change namespace parent linkage of BasicEnemy from enemy to Sprite.
modules/Station/1/scripts/enemy/basicEnemy.cs (22): Unknown command setUseMouseEvents.
Object (1433) enemy -> Sprite -> SpriteBase -> SceneObject -> BehaviorComponent -> DynamicConsoleMethodComponent -> SimComponent -> SimObject


Thanks in advance,

Jeff

#1
04/16/2014 (9:52 pm)
Interesting bug. (to answer the question: yes, class and superclass are allowed in T2D)

I ran some trials - you can give an object only a class, and there's no issue. You can give an object only a superclass, and there's no issue. You can give an object a superclass, and then a class, and there's no issue. But if you assign an object a class first, and then a superclass, you get the Namespace::unlinkClass error.

So for now in the snippet above, switch the order:
%newBasicEnemy = new Sprite()  
   {    
      superclass = "enemy";
      class = "basicEnemy";
   };

Let me know if that works for you.
#2
04/17/2014 (3:50 pm)
Ahhh ok =) Yep, switching the order did the trick. Thanks!
#3
04/17/2014 (4:21 pm)
wrong thread...
#4
04/17/2014 (5:14 pm)
Mike, are you sure you have the right post?
#5
04/17/2014 (5:34 pm)
NOPE! I totally posted on the wrong thread. I attribute this to being married, with a screaming child, playing games with said screaming child and the neighbor's daughter, thinking about my second kid on the way (shhhhh, a secret), and of course...whiskey. Disregard what you read. My above post will remain as a reminder...don't drink-parent-code-post. I shall now copy/paste my comment to the right thread.

Thanks Jeff.