Game Development Community

Class based iteration instead of entire scenegraph

by Andy Hawkins · in Torque Game Builder · 11/23/2008 (1:52 am) · 5 replies

I found this thread on iterating through the scenegraph...

www.garagegames.com/mg/forums/result.thread.php?qt=74181

... but what about if there is something on a class-based level to iterate through objects of the same class??

Is there something like this in TorqueScript...
foreach ( enemy in enemyClass )
{
      %x = enemy.getPositionX();
      %y = enemy.getPositionY();

      range = Math.abs( %x - $player.getPositionX() ) + Math.abs( %y - %player.getPositionY() );
      if (range < 10)
     {
            // do something
     }
}

#1
11/23/2008 (3:41 am)
I came up with this code (not tested yet) which made me start thinking about a container search. Would that be a possible solution? Maybe because container searches are optimised?

function FindClosestEnemy(%obj)
{
   %graph = sceneWindow2D.getSceneGraph();
   %count = %graph.getSceneObjectCount();
   %closestDist = 9999999999;
   %closestObject = -1;
   
   for (%i=0; %i<%count; %i++)
   {
      %enemy = %graph.getSceneObject(%i);
      if (%enemy.class == "enemyClass")
      {
          %x = %enemy.getPositionX();
          %y = %enemy.getPositionY();
          %range = mAbs( %x = %obj.getPositionX() ) + mAbs( %y = %obj.getPositionY() )
          if (%range <= %closestDist)
          {
              %closestObject = %enemy;
              %closestDist = %range;
          }
      }
   }
   return %enemy;
}
#2
11/23/2008 (3:00 pm)
When you run a "pick" function (container search) you can specify the group and/or layer of the objects that you are interested in. For example, if your enemy is of group type 10, you specify bit(10) in the group parameter and BAM, it filters the results to show only those from group 10.
#3
11/23/2008 (3:14 pm)
How would I configure a class to subscribe to a bit mapping then? I thought they were hard coding to object types like Static Object etc.
#4
11/25/2008 (4:22 pm)
You'd have to have a class-scoped "on level loaded" function that sets the graph group. When the object has it's own "on level loaded" function, you'd have to explicitly call the class function to make sure it's run.

function ThisClass::onLevelLoaded(%this, %SceneGraph)
{
  %this.SetGraphGroup(3);
}
#5
11/25/2008 (8:02 pm)
Too easy - thank you :)