Collisions/Enters with two overlapping area2ds are random

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

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?

:bust_in_silhouette: Reply From: DaddyMonster

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.

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.

mixtheleaf | 2022-01-25 18:41

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.

DaddyMonster | 2022-01-26 11:31

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.

mixtheleaf | 2022-01-26 16:58

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

DaddyMonster | 2022-01-26 17:15