0 votes

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.


in Engine by (78 points)
retagged by

1 Answer

+1 vote
Best answer

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...

by (21,800 points)
selected by

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

Thank you :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.