Game Maker: selecting multiple instances

I would like to show a simple method to work around a limitation of the Game Maker collision functions. This method works with all the collision functions that select an instance of an object, such as instance_place, instance_position, collision_line, etc..

These functions return the id of the object being collided if they succeed, otherwise return a negative value. But what about multiple collisions? The functions return only the id of the first object found.

So here is a trick to select multiple instances. The solution is very simple: just move the first instance found outside the room: at the next iteration, the collision function will return the next instance in the same position. At the end, just move the instances at their original positions.

Moreover, here is a function that returns an array containing the objects found, or -1 if no object was found. You can call it “instance_place_multiple”; the arguments are the same as instance_place.