On a shmup, bullets destroy two enemies if both are on the same position. How to fix it?

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

That’s, my problem. I’m trying to create a shmup in Godot, so for my bullet scene, I added an area_entered() signal pointing to a function that…

  1. Checks if the body which collided with the bullet is an enemy, so it explodes.
  2. Self queue frees the bullet.

Problem is that if two enemies are at same position and bullet reaches them, both are destroyed, when the “right” behavior in a classic shmup would be that only one would explode. Is there a way to deactivate the bullet detection as soon as it reaches the first collision it detects?

Thanks in advance! :slight_smile:

:bust_in_silhouette: Reply From: Jowan-Spooner

Hey Leandro,

I think those detection (and the emitting of the signals) are all done at once. But you could use the Array returned by get_overlapping_areas() to just delete the first item. This list should not update till the next physics frame, I believe.

Hope it helps. Good luck, your game looks pretty cool.

Thanks, Jowan. This is my current function…

Now how to pass the parameter to this "get_overlapping_areas()?

func _on_PlayerBasicShot_area_entered(body):
if body.has_method("got_hit"): #if the hit object has the function to explode it, execute it.
	body.got_hit()  #Destroy the enemy ship.
	queue_free() #Remove bullet from main.

Leandro | 2019-05-28 16:45

So I would just not use the original body variable but instead replace it by the first item in the array like this:

func _on_PlayerBasicShot_area_entered(body):
    body = $AreaName.get_overlapping_areas()[0]
    if body.has_method("got_hit"): 
        body.got_hit()  #Destroy the enemy ship.
        queue_free() #Remove bullet from main.

I’m not quite sure if that gives an error when trying to let it explode a second time. But a test should show :slight_smile:

Jowan-Spooner | 2019-05-28 17:25

:bust_in_silhouette: Reply From: Leandro

Silly me… Just added a counter in the scene to check if it was already used and problem solved. Now it detects only the first hit. :slight_smile:

func _on_PlayerBasicShot_area_entered(body):
if body.has_method("got_hit") and collisions==0: