Game Development Community

Trouble getting a list of specific object types

by eviltechie · in Torque X 2D · 08/09/2010 (3:24 am) · 3 replies

Hi,

I'm getting started with Torque X and it's going really well overall. But I've run into one thing I can't seem to figure out.

I'm currently building on top of the Blaster tutorial code while I test out Torque and get familiar with it. And, what I'm currently trying to do is get a ldscist of enemy ships. The goal being that I could then figure out which is closest or within a specific view angle, and build homing missiles.

I was looking at the info here:
http://docs.torquepowered.com/torquex/official/content/documentation/TorqueX%20Core/Concepts/Object%20Types.html

But that syntax didn't seem to work for me (more than likely I'm just missing something simple). I also search on these forums and noticed that some people mentioned that it may not be possible to detect objects generated from templates (in my case enemy templates created by a spawner). So, I'm a bit confused.

If I just want to get a list of objects of a specific type (enemy), so that I can loop through them looking at their x/y position, how would I do that?

If it isn't possible to do this with some version of FindObject, I had considered making the enemies write their current position to a global array that I could then read to get the info I want. Is there any major disadvantage to doing that?

#1
08/09/2010 (3:47 am)

// Get a target objectType 
TorqueObjectType TargetObjectType = TorqueObjectDatabase.Instance.GetObjectType("enemy");

// Get the nearest target object position
public Vector2 GetClosestTargetPosition(T2DSceneObject ourObject, TorqueObjectType targetObjectType, float maxRange)
{
            List<ISceneContainerObject> listEnemies = new List<ISceneContainerObject>();
            T2DSceneGraph SceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph");

            float closest = maxRange;
            int nearestObjectId = 0;

            if (SceneGraph != null)
                SceneGraph.FindObjects(ourObject.Position, maxRange, targetObjectType, (uint)0xFFFFFFFF, listEnemies);

            for (int i = 0; i < listEnemies.Count; i++)
            {
                float distance = Vector2.Distance(ourObject.Position, (listEnemies[i] as T2DSceneObject).Position);

                if (distance < maxRange)
                {
                    if (distance < closest)
                    {
                        closest = distance;
                        nearestObjectId = i;
                    }
                }
            }

            return listEnemies.Count != 0 ? (listEnemies[nearestObjectId] as T2DSceneObject).Position : Vector2.Zero;
}
#2
08/09/2010 (11:37 am)
Awesome! I'll try that out this evening. Just looking at it though I can see where the problems were in my approach.

Thank you!
#3
08/12/2010 (12:03 pm)
This worked perfect. With a couple modifications I now have missiles that seek the closest enemy target within their view angle (30 degrees in front of them).

Thanks again!