Getting a list of objects of a certain type
by Matthew Shapiro · in Torque X 2D · 09/19/2007 (5:43 am) · 4 replies
I can't figure out a way to get a list of T2dSceneObjects. I have 2-3 templates created in my scene which all are of type "npcEnemy". After much searching and going through tutorials the only method I could figure out for getting a list of every one of those templates was this:
This doesn't work though, as it never finds any object. I'm assuming this doesn't work because the objects are templates and thus do not actually exist yet. However, I can't find a more proper method of doing this. FindObject seems to only return one object, and I have to know the exact template name. I was hoping I could design it so that my game manager component spawns a random enemy (based on some parameters) and all I would have to do is just drag a new object and set it up.
Am I missing something?
// Get a listing of all enemy templates
// First reset enemytemplates variable
_enemyTemplates = new List<T2DSceneObject>();
// Find all objects in a 500 unit area around 0,0 of the enemy type. This is a hack unti I find a better method
List<ISceneContainerObject> foundObjects = new List<ISceneContainerObject>();
T2DSceneGraph.Instance.FindObjects(new Microsoft.Xna.Framework.Vector2(0, 0), 500000.0f, _enemyType, 0xFFFFFFFF, foundObjects);
// Put the templates into the _enemyTemplates variable
int x;
for (x = 0; x < foundObjects.Count; x++)
_enemyTemplates.Add(foundObjects[x] as T2DSceneObject);
// Make sure we found some templates
if (_enemyTemplates.Count == 0)
{
StringBuilder assertMessage = new StringBuilder();
assertMessage.Append("No enemy templates of types ");
assertMessage.Append(_enemyType.ToString());
assertMessage.Append(" found!");
Assert.Fatal(false,assertMessage.ToString());
}This doesn't work though, as it never finds any object. I'm assuming this doesn't work because the objects are templates and thus do not actually exist yet. However, I can't find a more proper method of doing this. FindObject seems to only return one object, and I have to know the exact template name. I was hoping I could design it so that my game manager component spawns a random enemy (based on some parameters) and all I would have to do is just drag a new object and set it up.
Am I missing something?
#2
Hrm thats an interesting idea, although I see headaches in trying to clone it and get the clones to function properly (and not as a template).
"Another idea: make a modified findobject(s) function which returns a list and allows you to specify an objectype and it can either search for normal objects or template objects."
Yeah I might just do this as it would be quite useful for quite a few things. I guess it's a good thing I bought the Pro version :D
09/19/2007 (3:04 pm)
"Another idea: don't make your template enemies actual templates, then add a custom component to them that onregister makes a templated copy of them AND adds them to some global or static list of "enemies" and then deletes the original."Hrm thats an interesting idea, although I see headaches in trying to clone it and get the clones to function properly (and not as a template).
"Another idea: make a modified findobject(s) function which returns a list and allows you to specify an objectype and it can either search for normal objects or template objects."
Yeah I might just do this as it would be quite useful for quite a few things. I guess it's a good thing I bought the Pro version :D
#3
The preferred method is to do the first thing James said: give the objects unique names and look them up in the TorqueObjectDatabase using FindObject.
On a related note, I recently added support to the TorqueObjectDatabase to search for all objects of a given type, all registered objects of a given type, and the first object or first registered object found of a given type (if, for example, you wanted to just find any 2d scenegraph). This is no longer restricted to TorqueBase derivatives either. Any type, even interface types (for example, ILightObject) can be searched for. It should be much easier to find what you're looking for. There are even versions of the list methods to which you can pass a reference to an existing list so as not to churn memory allocating new lists when doing frequent lookups. ;D
09/21/2007 (5:29 pm)
As James said, the reason it doesn't find any objects is that they aren't in the scene yet. A scene object is added to the scenegraph when it's registered. As template objects aren't registered on load, the scenegraph doesn't know about them.The preferred method is to do the first thing James said: give the objects unique names and look them up in the TorqueObjectDatabase using FindObject.
On a related note, I recently added support to the TorqueObjectDatabase to search for all objects of a given type, all registered objects of a given type, and the first object or first registered object found of a given type (if, for example, you wanted to just find any 2d scenegraph). This is no longer restricted to TorqueBase derivatives either. Any type, even interface types (for example, ILightObject) can be searched for. It should be much easier to find what you're looking for. There are even versions of the list methods to which you can pass a reference to an existing list so as not to churn memory allocating new lists when doing frequent lookups. ;D
#4
Awesome! :D
09/22/2007 (10:47 pm)
"On a related note, I recently added support to the TorqueObjectDatabase to search for all objects of a given type, all registered objects of a given type, and the first object or first registered object found of a given type (if, for example, you wanted to just find any 2d scenegraph). This is no longer restricted to TorqueBase derivatives either. Any type, even interface types (for example, ILightObject) can be searched for. It should be much easier to find what you're looking for. There are even versions of the list methods to which you can pass a reference to an existing list so as not to churn memory allocating new lists when doing frequent lookups."Awesome! :D
Associate James Ford
Sickhead Games
TorqueObjectDatabase.Instance.FindObject
I believe it only returns one object so once again you would have to give all your templates unique names.
Do you really want to spawn a completely template? What if you want to use a template to represent something other than an enemy? Also there are no "sceneGroups" in TorqueX like TGB (which could have allowed you to group different templates).
If you want to make a spawner than spawns something at random you can modify it to contain a List
TorqueObjectDatabase.Instance.FindObject
I don't know of a way to get a list of all objects of a type, although it could be handy.
Another idea: don't make your template enemies actual templates, then add a custom component to them that onregister makes a templated copy of them AND adds them to some global or static list of "enemies" and then deletes the original.
Another idea: make a modified findobject(s) function which returns a list and allows you to specify an objectype and it can either search for normal objects or template objects.