Author Topic: Clearing only a type of brick  (Read 1754 times)

Guys, start your for loop at the highest value and then work down to 0...
is this because objects that were created more recently have a higher ID or is that not true?
nevermind
« Last Edit: September 30, 2012, 06:22:36 PM by Scars75 »


Code: [Select]
%mct = MainBrickGroup.getCount();
for(%i = 0; %i < %mct; %i++)
{
%obj = MainBrickGroup.getObject(%i);
if(!%oct = %obj.getCount())
continue;
for(%x = 0; %x < %oct; %x++)
{
%b = %obj.getObject(%x);
if(%b.getDatablock().isTrenchDirt)
%b.delete();
}
}

One of the basic programming practices (applies to ALL languages) -- dont delete stuff from a list when your looping over it.
Try something like this instead:
Code: [Select]
%mct = MainBrickGroup.getCount();
for (%i = 0; %i < MainBrickGroup.getCount(); %i++)
  {
   %obj = MainBrickGroup.getObject(%i);
   %toDelete = new SimSet();
   for (%x = 0; %x < %obj.getCount(); %x++)
     {
      %b = %obj.getObject(%x);
      if (%b.getDatablock().isTrenchDirt)
        %toDelete.add(%b);
     }

   while (%toDelete.getCount() > 0)
     %toDelete.getObject(0).delete();
}

the funky while loop is because if you just do this:
for (%a=0; %toDelete.getCount(); %a++)
  {
   %obj = %toDelete.getObject(%a);
   %obj.delete();
  }

You will get the same errors - this is because when you delete an object, torque is smart enough to delete objects from any lists they are in. 

Personally, I use the tail-end method instead.

for(%i=%count-1;%i>=0;%i--)
   if(%condition)
      %group.getObject(%i).delete();

Personally, I use the tail-end method instead.

for(%i=%count-1;%i>=0;%i--)
   if(%condition)
      %group.getObject(%i).delete();

Exactly.
start your for loop at the highest value and then work down to 0...

or use while loops

while(isObject(%targetBrickGroup.getObject(0))) { %targetBrickGroup.getObject(0).delete(); }


this is rather bland, ignoring the checks if it's the right kind of bricks and stuff

You can also schedule the call with a delay of 0 if you prefer/need to do it in a loop from 0 and work up.

Red guys way worked
Thanks for all the help

Try something like this instead:
You're creating a different simset for every brickgroup, and then not deleting them when you're done.

The only problem with this that it lags a lot with a big build

Any way to prevent this

The only problem with this that it lags a lot with a big build

Any way to prevent this

Use schedules to do processing over time (as opposed to instantly).

Use schedules to do processing over time (as opposed to instantly).
How would I delay a for loop with schedules?

Use schedules to do processing over time (as opposed to instantly).
do you mean
Code: [Select]
for(stuff){
     schedule(%time, 0 eval("code");
}
or
Code: [Select]
schedule(%time, 0, eval("
for(stuff){
     code
}
"));

How would I delay a for loop with schedules?
Code: [Select]
function asdf(%current,%max)
{
    if(%current >= %max)
        return;
    brickgroup_666.getobject(%current).dostuff();
    $asdbfjs = schedule(1, 0, asdf, %current+1, %max);
}
asdf(0, brickgroup_666.getcount());
« Last Edit: October 04, 2012, 05:33:44 PM by Mold »