Check if object exists at position

K

KetoGames

Guest
I'd figure that this is a relatively simple question, however, I have yet to find an answer. Note, I've already tried using instance_place, however, it returns the instance id instead of true or false.

Short question: How can I see if obj exists at x, y, and if it does, return true?

Long question: I am creating a sandbox game (similar to Terraria, for those of you that have played before). I decided to embark on learning how to bitmask, which is something like this:

https://gamedevelopment.tutsplus.co...ng-to-auto-tile-your-level-layouts--cms-25673 .

My end goal is to make it so that every time I place a "tile" of one item (let's say wood, for this example) it will bitmask the sprite. That way, the end product will look much cleaner.

Any help is appreciated, thank you!
 
First of all, if you're just checking if the object exists at a certain position, you shouldn't be using instance_place. Use instance_position instead. If you use instance_place, the function uses the calling object's bounding box to check for collision, instead of just the existence of an object at a point.
With that being said, since you don't want the id of the object that is being checked, don't use instance_position, but instead use position_meeting.
These little tips are also all in the manual ;)
instance_place: With this function you can check a position for a collision with another instance or all instances of an object using the collision mask of the instance that runs the code for the check.
position_meeting: With this function you can check a position for a collision with another instance or all instances of an object. When you use this you are checking a single point in the room for an instance or an object.
 
A

ajan-ko

Guest
I'd figure that this is a relatively simple question, however, I have yet to find an answer. Note, I've already tried using instance_place, however, it returns the instance id instead of true or false.

Short question: How can I see if obj exists at x, y, and if it does, return true?

Long question: I am creating a sandbox game (similar to Terraria, for those of you that have played before). I decided to embark on learning how to bitmask, which is something like this:

https://gamedevelopment.tutsplus.co...ng-to-auto-tile-your-level-layouts--cms-25673 .

My end goal is to make it so that every time I place a "tile" of one item (let's say wood, for this example) it will bitmask the sprite. That way, the end product will look much cleaner.

Any help is appreciated, thank you!
use
position_meeting( x, y, obj );
so let's say you wan to check 32x32 grid

autogrid_value1=position_meeting( x, y-32, obj );
autogrid_value2=position_meeting( x-32, y, obj);
autogrid_value4=position_meeting( x+32, y, obj);
autogrid_value8=position_meeting( x, y+32, obj);
autogrid_final=autogrid_value1+autogrid_value2*2+autogrid_value4*4+autogrid_value8*8
Make sure your origin on the center
 
D

DariusWolfe

Guest
instance_position is what I would use. It also returns the instance ID, but you can just say something like "if instance_position(x,y,obj) != noone ..."

I just got back into GM since last using GM8, and I was making the same mistake (using instance_place)
 
J

jr carey

Guest
you could just do something like:

create event:
var xx = 0;

step event:
if(!place_empty(x,y))
{
xx = 1;
}
draw event:
draw_self();
draw_text(20,20,xx);


that way it will just let you know if theres an object there or not, all you have to do is get the instance id, which should be pretty simple
 

TheouAegis

Member
if instance_position(x,y,whatever)

is the same thing functionally as

if position_meeting(x,y,whatever)

except the first one is more flexible, since you can change the if to a with easily enough if you wanted to later.

instance_position() always returns a value of -4 or a value greater than 1000000. You will never get 0, 1, 2, 3, 4, ... etc, so instance_position() is functionally the same as position_meeting() when treated as a boolean.
 
L

lumini

Guest
I know I am late, like two months, but here is a simple code for the first question you asked. Hope it is clear. :potato: ;)

Code:
/// exists_at_Position(x,y,object);

/*
 Check if there is at least one speicified object at the given coordinates.
*/

var i,check, instNum;

check=false;
instNum = instance_number(argument2);

for (i=0;i<instNum && check==false ; i++)
{
    var inst = instance_find(argument2,i);
    
    if (inst.x==argument0 && inst.y==argument1)
        check=true;

}

return check;
 
Top