Game Development Community

SafeDelete dynamic objects or not?

by Christos Karapiperis · in Torque Game Builder · 08/29/2006 (1:21 am) · 3 replies

Hi to all.
My question has to do with the destruction of dynamic objects, those which are created with new

(e.g. $missile = new t2dStaticSprite() ).


I haven't checked if torque has a garbage collection mechanism, so do I have to use safeDelete() in the end of the program for every sprite I have created with new or torque handles their deletion itself? My c++ background won't let me be calm regarding the garbage collection.
Thanks in advence.

#1
08/29/2006 (4:42 am)
There is no garbage collection; TorqueScript isn't like Java or C#, it's more like C++ in that you must delete objects when you are finished with them.

With that said, there are some nice features that allow you to destroy objects in one go; try having a look at the "SimGroup" object. Essentially you can add an object to the group and if you delete the group, all the objects within it are destroyed as well. "SimSets" are similar but they don't destroy the objects they reference.

Also, when you shutdown the engine, it destroys all objects.

In TGB specifically, when you destroy a scene-graph, you can get it to destroy the objects that are within it.

Of course, if you're loading/destroying levels, you need to use either explicit object deletion with ".delete()" or ".safeDelete()", destroying via a SimGroup or destroying the scenegraph.

BTW: ".safeDelete()" is only available for TGB objects e.g. ones with the "t2d" prefix. ".delete()" can be used by all objects including TGB ones. The advantage of ".safeDelete()" is that it can be used when an object does a script-callback as it defers the deletion until the start of the next frame.

Hope this helps,

- Melv.
#2
08/30/2006 (1:00 am)
Thanks a lot for the answer.
#3
08/30/2006 (7:22 am)
Not a problem. Enjoy. :)

- Melv.