Game Development Community

Cycling through objects of the same obj.Name

by George Gibson · in Torque X 2D · 09/23/2008 (6:58 am) · 4 replies

I'm assuming that what I'm trying to accomplish here should be simple enough, though I can't seem to manage it. I have multiple objects cloned from a template on the scene, each of them with the same Name assigned to them. At the end of the "level", the goal is to clean up these objects before displaying information about the player's achievements and moving on to the next stage.

While that task seems like a no-brainer, I honestly can't get it down, apparently. I've attempted something to this effect:

T2DSceneObject objTrash = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Trash");

if(objTrash != null)
{
   while(objTrash != null)
   {
      objTrash.MarkForDelete = true;
      objTrash = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Trash");
   }
}

... which inevitably leads to the game seizing and I loose another brain cell. Gah!

Would someone please point me in the right direction here?

Thanks,
George

#1
09/23/2008 (10:05 am)
Short version: You're in an infinite loop--mark for deletion doesn't remove the object from the database, it marks it for later cleanup--so your while loop is finding the same object over and over again, forever.

If I remember correctly, there is a version of FindObject (FindObjects?) that returns a list of all matching objects from the database...you should then be able to iterate over that list and mark each for deletion.

Side note: It's normally a bad idea to have multiple objects with the same exact name--while not strictly enforced, the concept of naming an object is designed to provide an easy way to locate an exact, unique object by using a name reference. If you name many objects with the same "name", you'll have no real idea which object you are getting back with a FindObject--yes, you're getting an object with the correct name, but it might not actually be the one you want.

In other words, naming objects is designed with the assumption that each object will have a unique name. The use case your code segment suggests is that "trash" is a class identifier for an object, not a unique name for the object.

After post edit: After reviewing my message, I realized it's been a -long- time since I've looked at the underlying code for FindObject and markForDelete, so the above -could- be incorrect. I'm still reasonably confident that I'm right, but I wanted to put a disclaimer on it in case things have changed in the last couple of releases.
#2
09/23/2008 (12:07 pm)
Thank you very much; the following works perfectly:
List<T2DSceneObject> TrashList = TorqueObjectDatabase.Instance.FindObjects<T2DSceneObject>();
                foreach (T2DSceneObject Trash in TrashList.FindAll(isTrash))
                {
                    Trash.MarkForDelete = true;
                }

... with the tiny bool:

private static bool isTrash(T2DSceneObject Trash)
        {
            if (Trash.Name == "Trash")
                return true;
            return false;
        }

Perfect! Also, thanks for the advice on object naming. The program I've made is relatively tiny, so it isn't a huge deal here, but I won't approach a larger game with multiple objects named the same.

Praises and all,
George
#3
09/24/2008 (6:36 am)
Looks good for a small game--glad it worked out for you!

Some additional advice for larger games--you can pre-select what is returned by FindObjects based on I think at least ObjectType, which is a very fast test that FindObjects can perform itself. Currently, your List contains every single T2DSceneObject that exists in your game--workable for a very small game, but can quickly become quite cumbersome.

If you go ahead and create a new ObjectType and assign it to your trash objects, you can then supply this object type to the FindObjects call as a parameter, and the pre-selection is done for you...again, not a big advantage if you only have a few objects, but if the TrashList contains 2000+ objects, you're iterating over that list of objects twice, when you could optimize it a bit more quickly with an ObjectType as a pre-selection criteria.
#4
09/25/2008 (3:20 am)
I do not think that using == on strings is a good idea. I am not 100% sure about C# but this is likely a problem. When comparing strings do NOT use == but the .Equals() method.


if (Trash.Name.Equals("Trash"))
...

== usually compares references of objects an not the value they carry. In Java this would not work.