How to select one object when objects are overlaping?

Godot Version



I’ve created a drag and drop system utilizing events tied to left mouse button that switches “selected” state making the object follow the mouse. Here’s the issue. While this works wonders for a single object i wanna be able to “stack objects on eachother” and then be able to drag them one by one. Any tips? Sorry, first time posting, if any info is needed i’ll provide it.

You may need to implement your own clicking logic instead of using the GUI defaults for that. When the user clicks, make a list of all the items that contain that point and let the user cycle with whatever mechanism you want, like by clicking again while shift is held.

isn’t there a way to grab the top layer object?

here’s the code i’m running rn. As you can see, i’m using areas to lock in my objects to, while this works as intended as soon as i drag objects all of them start being dragged insted of just the top one.

They are rendered in Node Tree order, so the one on top will be last sibling iirc (if it’s the first, oops). But if they are on different tree depths you have to check the outermost level first. If you are using z-index this does not apply, obviously.

I’m still really new to godot and thus unfamilair with z-index and how things like that operate. Can you point me to some sources i could learn from?

z-index is just a property of every CanvasItem-derived object. It just tells godot what to draw first. If two items have the same z-index then the node tree order is accounted for.

This just need each of the object has a script of its own and it has a variable that will turn on and off (true and false) that always check, if there’s an item above it.
If there’s an item above it upon entering this item’s area2d, it will “lock” itself. But when an item above it is move out of the way, it will reenable itself by checking the move out of the way’s item’s z-index property is exactly 1 more than itself.

then you may ask how these item has their item’s z-index ordering correctly?
when you drag and then drop the item that want to be stacked, it will check the place it will drop and if there’s an item on the place it will drop, it will get the previous stacked’s item z-index and use that and add +1 to it so it will be on top.
or it could be checked during dragging and use on area 2d body enter/area enter signal to determine its z index during dragging. If it is canceled to drop, it will then refer to previous z-index to use. so this method will need you to save the previous z-index value other than the variable if it’s locked or not to pick/select.

1 Like

That’s an amazing idea and most likely my solution! Gonna have problems implementing it though :frowning:

Any idea how i can store information and check it so i keep track of z-index?

make a scene for the object you wanted to use, in that scene attach the script(manually from editor)
then just instantiate the object scene from code and assign the z-index value and store it in a variable

because we connect signal to this script example on area body/enter signal to a method, then each new instantiated object scene can do that

1 Like

I’ll try your suggestion. Thank you so much for suggestions all. This was a godsend

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.