Game Development Community

How do I get an instance of a template object in TXB?

by Jason Hardesty · in Torque X 2D · 11/13/2007 (3:45 pm) · 4 replies

Im trying to create a homing missle that the player can fire. I used some of the code from Blaster and SpaceShooter tutorials. This is what I have for the seeking for the missile:

protected void _UpdateHomingAI(T2DSceneObject projectileObj)
{

_enemy = (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("EnemyTemplate");
_projectileObj = TorqueObjectDatabase.Instance.FindObject("ProjectileTemplate");

// determine angle to point our homing missile to the enemy
float angle = T2DVectorUtil.AngleFromTarget(projectileObj.Position, _enemy.Position);

// set the rotation of our homing missile
projectileObj.Rotation = angle;

// set the homing missile on its way towards the enemy
projectileObj.Physics.Velocity = T2DVectorUtil.VelocityFromTarget(projectileObj.Position,
_enemy.Position, 5.0f);
}

The problem is that the homing missle goes for the Enemy Template and not the the object that is spawned using the spawner object in TXB. How can I get the instance of the template? When an instance of the template enemy is created is a name given to the instance so it can be referenced and if this spawner keeps spawning enemies do the enemies all have different names so they can be referenced?

Thanks,
Jason

#1
11/13/2007 (8:14 pm)
_enemy = (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("EnemyTemplate");

This will always set _enemy to your template if it's named "enemyTemplate". Spawned objects have their own name.


What I would probably do is create and object type for enemies. You do this by clearing your selections, clicking the edit tab, and add object types in the Scene Data section. Now set your template's object type to this same type. All spawned objects will inherit this type.

Now in your AI routine you can query the scene for objects of that type. Like this:

T2DSceneCamera cam = T2DSceneGraph.Instance.Camera as T2DSceneCamera;
            if (cam != null)
            {
                RectangleF screen = new RectangleF(cam.SceneMin, cam.Extent);

                TorqueObjectType type = TorqueObjectDatabase.Instance.GetObjectType("YourType");
                List<ISceneContainerObject> onScreen = new List<ISceneContainerObject>();
                T2DSceneGraph.Instance.FindObjects(screen, type, 0xFFFFFFFF, onScreen);

                foreach (ISceneObject o in onScreen)
                {
                    T2DSceneObject obj = o as T2DSceneObject;

                    if (obj != null)
                    {

                     //do something
                     ...

You would have to decide which enemy to target somehow.
#2
11/13/2007 (8:39 pm)
THANK YOU SO MUCH! that was what I was looking for. The only other problem I was having was with how it updates the projectile every time. I have UpdateHomingAI() called every process tick and it works great if I only fire one at a time. Now if I fire another one the new one will be updated every process tick but the old one will not. Any ideas? Should I do something like how you created a list of objects?
Thanks for your help
Jason
#3
11/13/2007 (8:51 pm)
Ok nevermind on the last question I just used the same method for updating the missile.
#4
11/14/2007 (1:17 am)
Download these new tutorials if you haven't yet. The third one covers a nice way to deal with projectiles. And that is to clone the template, set an initial velocity and rotation, then register it with the object database. For you case, you would write a homing ai component and assign it to the projectile template. That way each projectile you spawn will be able to track individual targets. Check these out and have fun.

www.garagegames.com/mg/forums/result.thread.php?qt=67749

peace.