Game Development Community

SimGroup::safeDelete for T2D

by Alex Rice · in Torque Game Builder · 10/13/2005 (12:00 am) · 1 replies

After reading another thread about t2d resource management, and knowing how much I am using SimGroups, it seemed like Simgroup::delete() should be aware of safeDelete() and T2D objects.

I don't know if this is sane or not, but it seems to work fine.

usage:

If you have a SimGroup with a bunch of stuff in it, then calling %yourSimGroup.safeDelete() will purge out the T2D objects with safeDelete() and then it will blow away the remaining SimGroup.


//----------------------------------------------------------------------
/// purpose: Safedelete() all T2D objects in the Set, then blow the remaining
///			 SimGroup away. Kablam!
/// params: -
/// returns: -
function SimGroup::safeDelete ( %this )
{
	warn("SimGroup::safeDelete => starting with objects");
	%this.listobjects();
	for (%i = 0; %i< %this.getCount(); %i++)
	{
		%tHandle = %this.getObject( %i );
		%tClassname = %tHandle.getClassname();
		if ( (strstr( %tClassname, "t2d" ) == 0) ||
			 (strstr( %tClassname, "fx") == 0 ))
		{  
			// this is probably a t2d object which has safeDelete() method
			%tCount = %this.getCount();
			%tHandle.safeDelete();
			if (%this.getCount() < %tCount) 
				// item has been removed from SimGroup already
				{}
			else
				%this.remove( %tHandle ); // don't want it remaining here
			%i--;  // there should be 1 less object in the SimGroup now
		}
	}
	warn("SimGroup::safeDelete => done scanning for t2d objects ");
	%this.listobjects();
	warn("SimGroup::safeDelete => nuking self");
	%this.delete(); // this seems dangerous but no problem so far
}

#1
10/13/2005 (1:43 am)
A fix for scanning for t2d objects because datablock objects don't have safeDelete()

function SimGroup::safeDelete ( %this )
{
	for (%i = 0; %i< %this.getCount(); %i++)
	{
		%tHandle = %this.getObject( %i );
		%tClassname = %tHandle.getClassname();
		if ( ( (strstr( %tClassname, "t2d" ) == 0) ||
			   (strstr( %tClassname, "fx") == 0 ))  [b] &&
			  ( strpos( %tClassname, "Datablock") < 0 ) [/b])
		{  
			// this is probably a t2d object which has safeDelete() method
			%tCount = %this.getCount();
			%tHandle.safeDelete();
			if (%this.getCount() < %tCount) 
				// item has been removed from SimGroup already
				{}
			else
				%this.remove( %tHandle ); // don't want it remaining here
			%i--;  // there should be 1 less object in the SimGroup now
		}
	}
	%this.delete(); // this seems dangerous but no problem so far
}