Game Development Community

T2D Class Creation Issues

by Ray Gebhardt · in Torque Game Builder · 03/05/2005 (3:47 pm) · 6 replies

I attempted to define set of functions, in a class namespace, and then create an instance of a fxAnimatedSprite2D using it. I did this in order to have my own custom functions on the sprite, and also to override the onCollision callback for that sprite instance. I did something like what you see below.

First i create a function in the Knight class namespace.

function Knight::onCollision(%srcObj, %dstObj, %srcRef, %dstRef,
                                             %time, %normal, %contactCount, %contacts)
{
   echo("Collision detected for knight");
}

The somewhere in code, I would create an animated sprite, using that class namespace.

%knight = new fxAnimatedSprite2D(Knight) {scenegraph = t2dSceneGraph;};

Now for some reason that callback is never called, and even if I call the method manually, it never gets executed. It will just give me a message saying that the member does not exist. Is it possible to "subclass" objects in T2D?

I mean I could use ScriptObjects, and then do work. Its just that my game's implementation wouldn't be as clean. Especially when it comes to implementing the callbacks to the individual classes.

#1
03/05/2005 (11:39 pm)
Well (right now) TorqueScript doesn't handle classes and namespaces in a serious / full object-oriented way. I say "right now" because this may be changing. And you can see Bryan Edds' stuff (check the recent resources here on the site) for some of this info.

So, you probably will want to use ScriptObjects. If you want more detailed help on how best to design your scripts, no prob... just post some more info about what exactly you're trying to do and myself or Melv (or Harold or Stephen or any of us that're really familiar w/ TGE) I'm sure will be happy to offer some thoughts.

Regarding collision callbacks, we'll likely be modifying things in the future such that you can set per-object and per-class functions.
#2
03/06/2005 (12:13 am)
Probably the easiest thing you could do right now would be to turn the onCollision functions into dispatchers.

Create a scriptObject for each collision class you want to handle and give them names. Something like this

%this = new scriptObject(bullet);
%this = new scriptObject(player1);
%this = new scriptObject(player2);
%this = new scriptObject(wall);
%this = new scriptObject(jar);


function fxStaticSprite2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	if (isObject(%srcObj.class))
	{
		%srcObj.class.onCollission( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts );	
	}
	else
	{
		// handle other collisions here
	}
}


function bullet::onCollision( %this, %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	//Handle Bullet Collisions
}	


function player1::onCollision( %this, %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	//Handle player1 Collisions
}	


function player2::onCollision( %this, %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	//Handle player2 Collisions
}	


function wall::onCollision( %this, %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
	//Handle Bullet Collisions
}

EDIT:
Whoops forgot a note.

When you create an object just add something like

%obj.class = player1;

to tell what collision class it is.
#3
03/06/2005 (1:55 am)
... or your dispatcher could use "getClassName()" on the object to find out what class it is which makes things even easier.

- Melv.
#4
03/06/2005 (2:22 am)
Thanks a lot for clarifying that for me guys.
#5
06/16/2006 (8:49 pm)
Now you can define oncollision functions for each class, i think...

obj_class::oncollision(%this, etc...)

is that true? I'm actually having problems with it
#6
06/16/2006 (9:14 pm)
Yes, you now have a namespace for the object's name, class, and superclass