Overlapping Area2d issue - mouse_entered/exited overwriting each other

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By alwaysusa

Hey all,

I have a building game and am currently limiting where the player can place an object based on a “CanBuild” boolean that is set when the mouse_ entered/exited signals from an area2D on the previously placed objects.

I place the new object just outside the restricted area2D (so that “CanBuild” is true), the objects will have overlapping area2Ds, both detecting the mouse_ entered and exited states. The objects are instanced scenes for reference.

The problem is I can build on the first scene, then build over the second scene, then back to the first ad nauseum. If the two objects are placed far enough apart that there is not overlap, everything works as expected.

I believe that the mouse exited signal on the new scene is being called AFTER the mouse entered on the first scene, which of course is setting CanBuild to true despite still being within the area2D of the first scene.

I am not sure how to resolve this. As this is a mouse_entered signal and not an overlapping body, I can’t build an array (can I?) - though potentially I could increment an int and keep count. I could also perhaps use a separate area2D to manage the build tolerance/detection, but it seems like an ugly solution.

Before I do anything like that, I wanted to check I wasn’t missing a built in simple solution.

Cheers!

:bust_in_silhouette: Reply From: jgodfrey

I’m not sure I fully understand, but it seems that you’re trying to ensure that you’re outside all of the other areas before allowing a new item to be dropped? And, this is complicated by the fact that the areas can be overlapped, which is causing some confusion with your management of mouse_entered and mouse_exited events?

On the surface, I’d probably try to increment an integer variable for every mouse_entered event and decrement that same variable for every mouse_exited event. Assuming that works properly, then you’d only want to allow a new item to be dropped when the variable’s value is0. That should indicate that you’re outside all existing areas…

I used an incrementing int as you suggested, and this solved the problem.

Thank you :slight_smile:

alwaysusa | 2020-05-12 23:20