Game Development Community

Class hierarchy

by Vern Jensen · in Torque Game Builder · 10/30/2007 (11:26 pm) · 2 replies

I'm a bit confused about how to create multiple levels of inheritance among objects. Lets' say I want to set up a class hierarchy like so:

- t2dAnimatedSprite
- Enemy
- Formation
- CircleFormation

Is this possible? I've managed so far to get the first 3 down, but when I try to do the CircleFormation, I get crazy errors I don't seem to be able to avoid:

"Namespace::unlinkClass -- cannot unlink namespace parent linkage for Formation for Enemy.
Error: cannot change namespace parent linkage of Formation from Enemy to t2dAnimatedSprite.

function createFormationOfType( %formationType )
{
	if ( %formationType $= "normal")
		return Formation::createInstance();
	else if ( %formationType $= "circle" )
		return CircleFormation::createInstance();
	else
		return -1;
}

function Formation::createInstance()
{
	%newSprite = new t2dAnimatedSprite() 
		{ 
			scenegraph = gameSceneGraph;
			superclass = Enemy;
			class = Formation; 
		}; 
	
	return %newSprite;
}


function CircleFormation::createInstance()
{
	%newSprite = new t2dAnimatedSprite() 
		{ 
			scenegraph = gameSceneGraph; 
			superclass = "Formation";	
			class = CircleFormation; 
		}; 
	
	return %newSprite;
}

#1
10/31/2007 (1:34 am)
Hi

You don't have a hierarchy because the scripting classes are not actual classes nor is torquescript object oriented. The use of superclass and class is restricted to those two per object and your object is stuck with those two by the moment you assign it. The error you are getting is because you are trying to reassing a new class to an object that already has one.

Although it looks like superclass is "higher" than class, both are simple namespaces for object methods. If both were renamed to namespace1 and namespace2, it would basically be the same and more obvious to the user.

There's a resource that allows namespace inheritance but you'll have to edit your source code. If, like me, you are unable to do that, you'll have to redesign your classes to acomplish the use of those two namespaces only.
#2
10/31/2007 (12:01 pm)
Alright, thanks for explaining that, Ricardo. Much appreciated.