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. When you use this you are effectively asking GameMaker to move the instance to the new position, check for a collision, move back and tell you if a collision was found or not.

In addition to objects and instances, the function also accepts:

Passing an array allows you to check for collisions against multiple objects and/or Tile Maps in one call.

This will work for precise collisions, but only if both the instance and the object being checked for have precise collision masks selected otherwise only bounding box collisions are applied.

This function will return the unique id of the instance being collided with, or the Tile Map Element ID of the tile map found, but if that is not needed it is slightly faster to use the function place_meeting(). If no collisions are found, noone is returned.

See: Collisions

 

Syntax:

instance_place(x, y, obj);

ArgumentTypeDescription
xRealThe x position to check for instances.
yRealThe y position to check for instances.
objObject Asset or Object Instance or Tile Map Element ID or ArrayAn object, instance, tile map ID, keywords all/other, or array containing these items

 

Returns:

Object Instance or Tile Map Element ID or noone

 

Example:

var _inst = instance_place(x, y, obj_Enemy);
if (_inst != noone)
{
    hp -= _inst.dmg;
    instance_destroy(_inst);
}

The above code will check for a collision with instances of "obj_Enemy" and if there is one, it will reduce the "hp" variable by the amount stored in the colliding instance's "dmg" variable and then destroy the colliding instance.