How to Select the Top Instance in a Pile

In this quick tutorial, we’ll learn how to detect only the front-most (top) instance when clicking on a pile of overlapping objects in GameMaker. Perfect for inventory systems, card games, or stack-based mechanics!
The Tricky Part
Let’s say you have several instances overlapping in the room. If they’ve all got a Left Click Event, and you click on them, every instance under the mouse will perform its action… but what if we wanted to only perform an action to the top card in the pile?
Click on a card to change its color. Try clicking on the different overlapping parts to see the issue!
The Goal
When you click on a pile of overlapping objects, you want only the one on top to respond — not all of them underneath.
How It Works
GameMaker draws and processes instances in a certain depth order — lower depth values are drawn on top. We can use that same logic to detect the top-most instance under the mouse.
We’ll:
- Create a function that grabs all instances at an x/y position.
- Loop through them and find the one with the lowest depth value.
- Return only that front-most instance.
- Apply the action to only that instance.
Setup the Project
- Create an object called
obj_card
. - Duplicate it a few times and stack them in the room so they overlap.
- Create a controller object called “obj_controller”, and place it anywhere in the room.
Each obj_card should be at different depths to simulate how they’d behave different gameplay.
The Function
Add a script (It doesn’t matter what it’s called) and add this code:
function get_top_instance(_x, _y, _object)
{
// Track the front-most object and its depth.
// It's set to 'noone' and the maximum farthest depth by default.
var _instance = noone;
var _depth = 10000;
// Cycle through each object
with (_object)
{
// Check if the object is at a given point
if position_meeting(_x, _y, id)
{
// If the depth is less than the front-most depth so far...
if depth <= _depth
{
// The instance you're looking for is this one!
_instance = id;
// ...and the front-most depth is this depth.
_depth = depth;
}
}
}
// If an instance was found, it'll return it, or it'll return 'noone'.
return _instance;
}
This is a function that any object can use. We’ll use a controller to call this function when the left mouse button is pressed to look for all instances of obj_card underneath the mouse, and return the top instance.
obj_controller’s Step Event:
// If the mouse was left-clicked...
if (mouse_check_button_pressed(mb_left))
{
// Get the front-most obj_card at the mouse's x/y.
var _target = get_top_instance(mouse_x, mouse_y, obj_card);
// If a target was found, change its color!
with _target
image_blend = make_color_rgb(irandom(255),irandom(255),irandom(255));
}
…and that’s really all there is to it! When the mouse is clicked, the top instance of obj_card will be chosen and its color will be randomized.
Another Consideration
In this simple example, we have the card’s action to be performed in the controller. If you had a complex game, you might want to do something like:
if (mouse_check_button_pressed(mb_left))
{
// Get the top instance of obj_card at the mouse's x/y.
var _target = get_top_instance(mouse_x, mouse_y, obj_card);
// If a target was found, change its color!
with _target
on_click();
}
and then in the obj_card instance, have a function called on_click() that performs an action. This keeps things a bit more flexible when many objects can be clicked that have a variety of actions that can happen.
Customize It
You can modify the action:
- Highlight the top card
- Bring it to the front
- Drag it
- Flip it, rotate, etc.
Bonus Tip: Auto-Depth Sorting by Y
To make stacks feel more natural, have each card automatically set its depth in the End Step
event:
depth = -y;
That way, cards closer to the bottom of the screen are drawn on top, just like a real tabletop!
Summary
- Use
a controller object
to get all instances under the mouse - Pick the one with the lowest depth (front-most instance)
- Interact only with that instance
This technique keeps your interactions tight, predictable, and professional; no more selecting hidden objects by accident.
Looking to take your skills even further? Learn how to do more cool things in GameMaker!