Game Development Community

how do make a for...in loop in Torque to find objects?

by Jason Oda · in Torque Game Builder · 04/30/2009 (4:00 pm) · 5 replies

I'm a flash guy trying to do what in action script is a "for...in" loop like this:

//kill all enemies
for( all in this){
if(this[all].enemy==true){
//kill enemy
}
}

how do I do this in torque? I'd like to say: for everything on screen, if it's class is "enemy", safeDelete()...or something.

Thank you very much for this basic knowledge. Having trouble finding it in the documentation.

#1
04/30/2009 (4:08 pm)
function YourFunctionHere()
{
     for( all in this)
     {
          if (%this.enemy == true)
          {
               //kill enemy
          }
     }
}

When you specify a variable, always start it with either % or $.

% means the scope is for that function only.
$ means the scope is the entire game.

I am not sure what the [all] thing is, but there is none of that in Torque.
#2
04/30/2009 (4:27 pm)
This function is causing the game not to launch due to code errors. My understanding was that there were not "for...in" loops in torque. only "for" loops. That's why I was asking this question. Any other suggestions?

Thanks.
#3
04/30/2009 (4:54 pm)
Look up SimSets and SimSet.getCount(), or use SimSet::ForEach() from TSTK: roaminggamer.com/tag/torque/
#4
04/30/2009 (5:46 pm)
Thanks Ronny! got it. Clunky as hell, but there it is...!
#5
05/01/2009 (11:38 am)
Yes, SimSets is the way.