Game Development Community

counting objects of 1 class in a trigger

by Louis Marchant · in Torque 3D Beginner · 10/16/2012 (8:46 am) · 5 replies

ok, so i have this trigger, that detects if a unit is inside it, and if it is, adds 1 to a counter ever tick.(resource collection RTS style)

function trigger_money_blob::onTickTrigger( %this, %trigger, %obj )
{
   // Print a string to the console window.
	echo("Player has entered the money blob.");
   //count collectors here

   //add money
	numericalHealthHUD.text=(numericalHealthHUD.text+1);
}

now atm that just adds 1 money every tick while there is an actor inside the trigger, regardless how many are inside.

what i need to do is count the number of collector class units inside(ignoring other classes)
i can check if it's collector, or count the items, what i'm struggling with is the best way to combine the 2 without some innefficient long loop.

thanks.

#1
10/16/2012 (9:09 am)
You'll still have to loop through the objects in the trigger to get their class to see if you should increment.
#2
10/16/2012 (9:14 am)
you could increment a multiplier in the onEnterTrigger and decrement it in the OnExitrigger.

#3
10/16/2012 (10:05 am)
yes i suppose onenter + and onexit - , then jsut have to check class, would be an option. - although i want to exploit the ticks

but i guess i was essentially right, in the only option is to create a loop, guess the SQL experience in me was hoping there was a way to do both in a simple line, as SQL could, but guess i was expecting too much lol :) loop it is.

thanks for replies
#4
10/16/2012 (10:22 pm)
Define a method in script on the class you want that does your work:
function myClass::myWorkerFunc(%this)
{
  // do stuff...
}
then in your onTickTrigger() callback:
%count = %trigger.getNumObjects();
%i = 0;
while (%i < %count)
{
  %obj = %trigger.getObject(%i);
  if (%obj.isMethod("myWorkerFunc"))
    %obj.myWorkerFunc();
  %i++;
}

Not sure if it's faster than getClassName(), but there is a speed advantage to using while instead of for because of how for is handled in the script interpreter.

Overall, it should be pretty quick unless you're expecting to have dozens of objects in a trigger at a particular time. Also, don't forget you can set the tick frequency in the trigger's datablock.
#5
10/17/2012 (6:41 am)
so define a method, that way if it doesnt exist it can't run and must be wrong class, yea i guess that makes sense.
and thanks for the while/for speed tip

thanks.