Author Topic: [SOLVED] Loop is loving up  (Read 1691 times)

I tried everything, enlighten me forums, whats wrong?
       function Vehicle::delete(%vehicle){
            if(isObject(%vehicle.brickGroup))
            {
                echo(%vehicle.brickGroup.getcount());
                for(%i=0;%i<%vehicle.brickGroup.getCount();%i++)
                {
                    echo(%i);
                    %obj = %vehicle.brickGroup.getObject(%i);
                    if(isObject(%obj))
                        %obj.delete();
                }
                %vehicle.brickGroup.delete();
            }
            parent::delete(%vehicle);
        }


Basically there's 25 bricks in the group but it only goes up to 12. It's weird. It echos 25 but then only goes up to 12.
« Last Edit: December 16, 2012, 12:48:54 AM by Brian Smithers »

Woah, brian smith got stuck by this too?
It happens to the best of us I guess.

Here's the deal: %obj[0], when deleted, moves %obj[1] to %obj[0]'s place.
When the loop increments, it ends up deleting %obj[2], now %obj 1, skipping %obj[1], now %obj 0.

Solution: use a while loop
While(isobject(%obj[0]))
%obj[0].delete();

Boom, done.
« Last Edit: December 15, 2012, 09:07:48 PM by Lugnut »

Because when you delete an object in the group the count goes down, so it won't go all the way up.
Use lug's suggestion.

Woah, brian smith got stuck by this too?
It happens to the best of us I guess.

Here's the deal: %obj[0], when deleted, moves %obj[1] to %obj[0]'s place.
When the loop increments, it ends up deleting %obj[2], now %obj 1, skipping %obj[1], now %obj 0.

Solution: use a while loop
While(isobject(%obj[0]))
%obj[0].delete();

Boom, done.
Wait.. forget

Seriously? That simple?
I got stuck on this myself, I thought about posting on the forums before just trashing the project

stuff son... Thanks Lug.

Alternatively I think you can start at the end of the group and then decrement each iteration.

Woah, brian smith got stuck by this too?
It happens to the best of us I guess.

Here's the deal: %obj[0], when deleted, moves %obj[1] to %obj[0]'s place.
When the loop increments, it ends up deleting %obj[2], now %obj 1, skipping %obj[1], now %obj 0.

Solution: use a while loop
While(isobject(%obj[0]))
%obj[0].delete();

Boom, done.

That is what I thought the error was, but I didn't think of that solution.
Clever, thanks. I'll test in a bit.

I've had wierd problems like this before. Put continue; at the end of the loop.

Just start at the end of the loop (the highest value) and work your way down to 0.

Just start at the end of the loop (the highest value) and work your way down to 0.
Thats what I ended up doing. thanks guys
I'll add [solved]

While(isobject(%obj[0]))
%obj[0].delete();

That would yield an error when the group finally is empty.

while ( group.getCount() )
{
   group.getObject( 0 ).delete();
}


I dunno why, but ok.

If you have a group with 0 objects, trying to get the object at index 0 would yield an out of range error (0 >= 0).

If you have a group with 0 objects, trying to get the object at index 0 would yield an out of range error (0 >= 0).
Come on Port, it's basic logic. The while(isObject(%obj0)) in Lugnut's code prevents the loop from running if the object doesn't exist. We're just talking about a Torquescript array here (%obj[0], %obj[1], etc), not about a group.

We're just talking about a Torquescript array here (%obj[0], %obj[1], etc), not about a group.

I tried everything, enlighten me forums, whats wrong?
        function Vehicle::delete(%vehicle){
            if(isObject(%vehicle.brickGroup))
            {
                echo(%vehicle.brickGroup.getcount());
                for(%i=0;%i<%vehicle.brickGroup.getCount();%i++)
                {
                    echo(%i);
                    %obj = %vehicle.brickGroup.getObject(%i);
                    if(isObject(%obj))
                        %obj.delete();
                }
                %vehicle.brickGroup.delete();
            }
            parent::delete(%vehicle);
        }


Basically there's 25 bricks in the group but it only goes up to 12. It's weird. It echos 25 but then only goes up to 12.


I was just referring to Lugnut's code.

Anway, to solve that problem you could just do this:
Code: [Select]
%vehicle.brickGroup.deleteAll();or this
Code: [Select]
%vehicle.brickGroup.chainDeleteAll();