[Need Help] - Recursive Object Search
by Nicolai Dutka · in General Discussion · 03/17/2010 (4:55 pm) · 6 replies
I need to search through my 'MissionGroup' folder, recursively to ALL sub-folders, so I can get a count of a specific object (in this case, terrain files).
I am using T3D and we have a lot of high poly scenes in our level, so we've split the level into 7 pieces, each piece getting its own terrain and being located 1000 meters above the previous area (except that area 1 is at Z position 0 and area 2 is at Z position 2000, every subsequent terrain is placed exactly 1000 meters above the previous so that area 7 is at 7000 meters).
I am writing some code that needs to know how many areas we have. I need this number to be dynamic so it will be compatible with future levels which could have more or less areas. I want to avoid having to set a dynamic field in the missionInfo object or some other such thing because designers are terrible at remembering to change those values manually.
So, I thought I had a solution, but I guess Torque doesn't like array values in the 'isObject' function:
This code clearly shows what I am trying to accomplish, but doesn't work because isObject(hazards[%i]) gives a syntax error. I know I've used arrays in other, similarly strange, ways, so I guess I was just hoping... :P
Anyway, what would be the best way to go about doing this? My thought was to recursively scan the entire 'MissionGroup' folder (and all sub-folders), count the number of terrain objects it finds, and use that as my 'throttle':
The only thing I need for that to work is a means to count the number of terrains in the current level...
I am using T3D and we have a lot of high poly scenes in our level, so we've split the level into 7 pieces, each piece getting its own terrain and being located 1000 meters above the previous area (except that area 1 is at Z position 0 and area 2 is at Z position 2000, every subsequent terrain is placed exactly 1000 meters above the previous so that area 7 is at 7000 meters).
I am writing some code that needs to know how many areas we have. I need this number to be dynamic so it will be compatible with future levels which could have more or less areas. I want to avoid having to set a dynamic field in the missionInfo object or some other such thing because designers are terrible at remembering to change those values manually.
So, I thought I had a solution, but I guess Torque doesn't like array values in the 'isObject' function:
function initHazards() {
for(%i=0; isObject(hazards[%i]); %i++){
%count = hazards[%i].getCount();
for(%i2=0; %i2<%count; %i2++)
{
%obj = hazards[%i].getObject(%i2);
%obj.setHidden(0);
}
}
}This code clearly shows what I am trying to accomplish, but doesn't work because isObject(hazards[%i]) gives a syntax error. I know I've used arrays in other, similarly strange, ways, so I guess I was just hoping... :P
Anyway, what would be the best way to go about doing this? My thought was to recursively scan the entire 'MissionGroup' folder (and all sub-folders), count the number of terrain objects it finds, and use that as my 'throttle':
function initHazards() {
%areaCount = getTerrainCount();
for(%i=1; %i-1<%areaCount; %i++){
%folder = "hazards" @ %i;
%count = %folder.getCount();
for(%i2=0; %i2<%count; %i2++)
{
%obj = %folder.getObject(%i2);
%obj.setHidden(0);
}
}
}The only thing I need for that to work is a means to count the number of terrains in the current level...
#2
Regardless, it's cool that you got it working I had just never thought about doing that and am not even sure if I'm interpreting it correctly. :)
03/17/2010 (7:16 pm)
I'm really confused by this line of code:for(%i=0; isObject(hazards[%i]); %i++)Doesn't isObject return a 0 or 1? So essentially it's the same as
if( isObject(hazards[%i])since if it returns 0 it skips the loop and if it returns 1 it goes through it once?
Regardless, it's cool that you got it working I had just never thought about doing that and am not even sure if I'm interpreting it correctly. :)
#3
PS: That code will cycle through all the files until it finds an object that doesn't exist. Like I said, it doesn't work, but in theory, it would check to see if "hazards1" is an object and if so, continue. If not, it will check "hazards2", and so on infinitely ("hazards9999999999999...") until it finds a single one that doesn't exist. So, the level designers have to be sure they don't skip any numbers!
03/17/2010 (7:19 pm)
@Patrick, you're looking at code that doesn't work... I only posted that to try and illustrate what I was attempting to accomplish. Look at my own reply and see how I got it working. ;)PS: That code will cycle through all the files until it finds an object that doesn't exist. Like I said, it doesn't work, but in theory, it would check to see if "hazards1" is an object and if so, continue. If not, it will check "hazards2", and so on infinitely ("hazards9999999999999...") until it finds a single one that doesn't exist. So, the level designers have to be sure they don't skip any numbers!
#4
03/17/2010 (7:20 pm)
Regardless, this is the same logic that confuses me:for(%i=1; isObject(%folder); %i++)
#5
Will cycle through the %folder infinitely until it finds an object that does not exist.
Think of a 'for' loop like this:
for('initialize the variable'; 'IF statement'; 'if the IF statement returns true, increment the Variable')
03/19/2010 (12:06 pm)
for(%i=1; isObject(%folder); %i++)
Will cycle through the %folder infinitely until it finds an object that does not exist.
Think of a 'for' loop like this:
for('initialize the variable'; 'IF statement'; 'if the IF statement returns true, increment the Variable')
#6
The concept of of some sort of equality in the middle argument of the for loop is so ingrained for me it was difficult for me to comprehend. It's a very neat concept.
Thanks.
P
03/22/2010 (3:19 am)
That's interesting Nicolai. The concept of of some sort of equality in the middle argument of the for loop is so ingrained for me it was difficult for me to comprehend. It's a very neat concept.
Thanks.
P
Torque 3D Owner Nicolai Dutka
function initHazards() { %folder = "MissionGroup"; for(%i=1; isObject(%folder); %i++){ %folder = "hazards_"@%i; if(isObject(%folder)){ if($devmode) echo("initHazards:: Found Hazards folder: "@%folder); %count = %folder.getCount(); for(%i2=0; %i2<%count; %i2++) { %obj = %folder.getObject(%i2); if(isObject(%obj)){ if($devmode) echo("initHazards:: Found Hazard Object: "@%obj); %obj.setHidden(0); } } } } }Echo statements came out:
I verified it is returning valid objects by running this:
function initTelekinetics() { %folder = "MissionGroup"; for(%i=1; isObject(%folder); %i++){ %folder = "telekinetics_"@%i; if(isObject(%folder)){ if($devmode) echo("initTelekinetics:: Found Telekinetics folder: "@%folder); %count = %folder.getCount(); for(%i2=0; %i2<%count; %i2++) { %obj = %folder.getObject(%i2); if(isObject(%obj)){ if($devmode) echo("initTelekinetics:: Found Telekinetic Object: "@%obj); %obj.setHidden(0); } } } } }Which gave me:
Kick ass, now I can run specific functions on each object type, no matter where they are in the MissionGroup! WOOT!!