for loop and instance_exists question

Bentley

Member
I was just wondering the following: if I put: place_meeting(x + i, y, obj_wall) as the expression in my for loop, is it then unnecessary to check if that wall exists?

For example, let's say I want to blow up every wall to my right:

Is this OK?
Code:
for (var i = 1; place_meeting(x + (i * 32), y, obj_wall); i += 1)
{
    var wall = instance_place(x + (i * 32), y, obj_wall); //Is it garunteed this wall exists b/c of the check already done by the for loop?
    instance_destroy(wall);
}
or do I need...
Code:
for (var i = 1; place_meeting(x + (i * 32), y, obj_wall); i += 1)
{
    var wall = instance_place(x + (i * 32), y, obj_wall);
    if (instance_exists(wall)) //I think this is redundant. Am I correct?
    {
        instance_destroy(wall);
    }
}
Thanks for the help.
 
Last edited:

YoSniper

Member
place_meeting only returns true if there is an instance of obj_wall to collide with, so yes, the instance_exists part is redundant.

HOWEVER, just because you would collide with that object does not mean that instance_place would detect it, because instance_place assumes that that object is EXACTLY at the given x and y.

Instead of using place_meeting, which returns true or false, and instance_place, which is incredibly specific, I recommend using a collision function that returns the ID of a corresponding object if it collides with the defined region.

I would rewrite your code like this:
Code:
var ii, inst;
ii = 1;
while true {
    //I assumed you meant "times 32" instead of "plus 32" because otherwise you wouldn't need the parentheses
    inst = collision_point(x + 32 * ii, y, obj_wall, false, true);
    if not instance_exists(inst) {
        break; //Breaks out of the otherwise infinite loop
    }
    with(inst) {
        instance_destroy();
    }
    ii += 1;
}
 

TheouAegis

Member
Um, instance_place does not assume anything is at a specific location. It's just place_meeting but returning an ID instead of a Boolean. The only drawback his coat has is that if there are enough walls Within the Collision, one or more could potentially get skipped. It's unlikely, but it is possible.
 

YoSniper

Member
Um, instance_place does not assume anything is at a specific location. It's just place_meeting but returning an ID instead of a Boolean. The only drawback his coat has is that if there are enough walls Within the Collision, one or more could potentially get skipped. It's unlikely, but it is possible.
You're right. I was thinking of instance_position.
 

Bentley

Member
place_meeting only returns true if there is an instance of obj_wall to collide with, so yes, the instance_exists part is redundant.

HOWEVER, just because you would collide with that object does not mean that instance_place would detect it, because instance_place assumes that that object is EXACTLY at the given x and y.

Instead of using place_meeting, which returns true or false, and instance_place, which is incredibly specific, I recommend using a collision function that returns the ID of a corresponding object if it collides with the defined region.

I would rewrite your code like this:
Code:
var ii, inst;
ii = 1;
while true {
    //I assumed you meant "times 32" instead of "plus 32" because otherwise you wouldn't need the parentheses
    inst = collision_point(x + 32 * ii, y, obj_wall, false, true);
    if not instance_exists(inst) {
        break; //Breaks out of the otherwise infinite loop
    }
    with(inst) {
        instance_destroy();
    }
    ii += 1;
}
I like your idea. Thanks for the reply. And yes, you are right, I meant times 32.
 
Top