Author Topic: Deleting all of an object.  (Read 659 times)

How would I go about deleting all of an object?
I have this but is there a better way?
Code: [Select]
for(%a = 0; %a = isObject("Object"); %a++)
{
Object.delete();
}

A simpler way of doing that:
Code: [Select]
while(isObject("Object"))
{
Object.delete();
}

Wait, what exactly are you trying to do? A type of Object, like all Players or Bricks? Because that's different.

Code: [Select]
for(%a = 0; %a = isObject("Object"); %a++)
{
Object.delete();
}
That will not work for 3 reasons.
1.  That is not the correct syntax for a for loop.
A for loop is really just a while loop, but with a counter built in.
Example:
Code: [Select]
for(%a = 0; %a < 5; %a++)
{
}
is the same as:
Code: [Select]
%a=0;
while(%a < 5)
{
    %a++;
}
The correct syntax is this:

1st expression sets the beginning counter variable. like %a = 0; or %oifaofje = 2938;, they both work.

The second expression is really an IF statement.
Example:
Code: [Select]
%a < 5The loop will keep going as long as that statement returns true, but what you had; %a = isObject("Object");, will not return a true or false, it will give a syntax error.

The third expression is what to do after each term in the loop.
The most commonly used ones are %var++; and %var--;.

2.  The point of a loop is to do something multiple times.
If the object your affecting disappears every time the loop goes, if you try to delete a non-existent object, Blockland will throw it's hands up in dispair and give you a annoying error message saying "no such object!". If you're trying to delete multiple objects, you need to have the code delete a new object every time.

3.  A string is not an object.
It will always return false.
You need to give it an actual object, like a player object or brick, for it to confirm it's an object.

No this post is not huge. It's just big enough.

%a = isObject("Object");, will not return a true or false, it will give a syntax error.
Actually, it will return false, unless he has an object named Object in which case it will return true.
Used in the loop, it will do nothing, unless he has an object named Object in which case it will run until Object no longer exists

So basically it's an odd way of doing it, but it will work correctly.
You could get rid of the '%a =' part of the condition and just have 'isObject("Object");' as the condition, or you could do what Amade posted, and you'd have to manually increment a variable in each iteration if you want to keep track of how many objects were deleted
« Last Edit: November 23, 2011, 05:31:24 PM by Headcrab Zombie »

1.  That is not the correct syntax for a for loop.
Yes it is, it'll work just fine.
Here's a for loop I've used before:
Code: [Select]
for(%currTime = formatDate(getDateTime()); DateGreater(%currTime, $CycleBasis); $CycleBasis = DateAddition($CycleBasis, $CycleLength))
{
$cyclesPassed++;
}
While you could argue its messiness, it is syntactically correct and does what it's supposed to.

2.  The point of a loop is to do something multiple times.
If the object your affecting disappears every time the loop goes, if you try to delete a non-existent object, Blockland will throw it's hands up in dispair and give you a annoying error message saying "no such object!". If you're trying to delete multiple objects, you need to have the code delete a new object every time.
Since the loop is using isObject it isn't possible to create an error because the object does not exist.

3.  A string is not an object.
It will always return false.
You need to give it an actual object, like a player object or brick, for it to confirm it's an object.
If an object is given a name (such as localClientConnection) it can be referenced by its name.

Amade beat me to it, ip you should know what you are talking about before trying to teach.  :cookieMonster: