0 votes

I have an area2d appear at a place where there's two other area2ds with overlapping hitboxes. This causes the area_entered signal for the first area2d to report one of the overlapping area2ds before the other(which one it is is completely random). Is there any way to make sure that which area2d is reported isn't random?

Godot version 3.4
in Engine by (42 points)
edited by

1 Answer

0 votes

I'm not clear on what circumstances would require multiple identical areas on top of one another but I assume you have a good project specific reason.

Both will fire on the same frame but the question of which will signal will arrive first is a race condition. Just ignore one of them, or better still, delete it altogether... Without understanding why you're doing this it's very hard to advise you. You could do yields or state handling, you could have this happening on different mask/layers but I'd need you to elaborate on the context to suggest something constructive.

by (2,154 points)

I can provide some context.
I have a projectile which is an area2d that detects anything it can hit, in this case a body and a "shield", both area2ds. The body and shield are overlapping, and when i spawn the projectile, it spawn right on top of the body and shield at the same time. Since the projectile is deleted when it hits something after dealing damage to it, i want to ensure that the projectile only deals damage to the shield and not the body, since the shield is graphically in front of the body.

One option would be to manage state: deactivate the body's collision on the bullet layer until the shield is down. That way while the shield is up only the shield is hit. When this happens you switch state and deactivated the shield collision and activate the body's. Now the bullets only hit the body.

For player bullets spawned in the body, they should be on a different layer.

okay i figured out a solution - When the projectile hits something and it's a body, I just ask the body for its shield and check if the projectile collides with the shield, if so then the shield gets damaged and not the body. It's a pretty meh solution but it works just fine.

Yeah, that'll work. It was a hard one to give you a solution when it's so project specific but it's good you sorted it. If it works, it's a win. :)

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.