Game Development Community

SimSet Silliness

by Kevin James · in Torque Game Builder · 06/01/2008 (5:14 am) · 4 replies

I have this code working to my heart's content . . .

function drawTriangularWaveRegression(%startX, %stopX)
{
   $graphSimSet = new SimSet() {};    
   
   for(%i = %startX; %i < %stopX; %i++)
   {
      %dot = new t2dStaticSprite()
      {
         scenegraph = sceneWindow2D.getSceneGraph();
         imageMap = boxImageMap;
         size = "1 1";
         position = %i SPC doTriangularWaveRegressionComputation(%i);  
      };
      
      $graphSimSet.add(%dot);
   }
}

function deleteGraphObjects()
{
   for(%i = 0; %i < $graphSimSet.getCount(); %i++)
   {
      $graphSimSet.getObject(%i).delete();  
   }
   
   $graphSimSet.delete();
}

. . . except that when I call deleteGraphObjects(), every other point is deleted and every other point is not. I want every point to be deleted. I don't have a whole lot of experience with SimeSets. What am I missing?

About the author

Computer security, digital forensics, and platform jumper enthusiast. shells.myw3b.net/~syreal/


#1
06/01/2008 (6:42 am)
Try change this

function deleteGraphObjects()
{   
   while ( $graphSimSet.getCount() > 0 )   
   {      
         $graphSimSet.getObject( 0 ).delete();     
   }      

  $graphSimSet.delete();

}
#2
06/01/2008 (6:58 am)
Your "deleteGraphObjects()" is doing something bad. You're destroying the (n)th object in a set that is continually decreasing. A much safer way of doing this (not just for sets) is to remove the first/last element continually until the container is empty.

In the case of a SimSet it's easier to continually remove the first object. Also note that you're not actually removing an object from the SimSet directly, you're simply deleting the object. Luckily the SimSet registers with the object and knows when it has been deleted and automagically removes it from the list. This means that if you deleted one of these objects elsewhere, they'd be removed from this set. To remove an object from the SimSet without deleting it you can use the "set.remove(obj)" method.

Anyway, here's a way to do it:
while ( $graphSimSet.getCount() > 0 )
   {
      // Fetch Object.
      %obj = $graphSimSet.getObject(0);
      // Delete Object.      
      if ( isObject(%obj) ) %obj.delete();    
   }
   // Make sure set is empty for next time.
   $graphSimSet.clear();
Also, if you can guarantee that all the sets occupants are t2dSceneObject subclassed objects then you should use ".safeDelete()". That's really only necessary if you're calling the "deleteGraphObjects()" from a callback where one of the objects is passed.

Also, things like the "clear" above can be overkill but I prefer defensive programming. ;)

Hope this helps,

Melv.
#3
06/01/2008 (12:31 pm)
Thank-you Melv and Aun. I have it working thanks to your help! I realize my blunder now, and why it was deleting every other object. I was thinking of a SimSet index as a static thing when it is not.

Thanks again! ;)
#4
06/01/2008 (12:38 pm)
You're welcome. :)

Melv.