I am making some sort of bullet grazing system and for very specific reasons, I don't want the player to be able to graze multiple bullets at the same time, so I tried to pick the closest bullet on the graze's area2D but I haven't found a solution for it.
This is the code that I am using for checking graze:

func grazing()
    grazedBullets += 1
    print(grazedBullets)

func _on_graze_area_area_entered(area):
	if area.itsGrazed == false:
		grazing()
		area.itsGrazed = true

I also tried searching code for checking instances but I didn't know how to applied them on an area2D.

  • xyz replied to this.

    boboboru When a bullet enters the graze area, add it to the array (or group) of grazed bullets. When it exists, remove it from that array (or group). Each frame iterate through array (or group) of grazed bullets and find the one nearest to the player.

      xyz This is what i have so far but it doesn't work know for some reason:

      func _on_graze_area_area_entered(area):
      	if grazePauseTimer <= 0:
      		if area.itsGrazed == false:
      			area.add_to_group("graze")
      			area.itsGrazed = true
      			grazePauseTimer = 2.0 / 60.0
      	
      	var grazeGroup = get_tree().get_nodes_in_group("graze")
      	var nearestGraze = grazeGroup[0]
      	
      	for bullet in grazeGroup:
      		if bullet.global_position.distance_to(grazeBox.global_position) < nearestGraze.global_position.distance_to(grazeBox.global_position):
      			nearestGraze = bullet
      			nearestGraze.remove_from_group("graze")
      			grazing()

        boboboru nv it actually works but I sometimes get this error and game crashes:
        Invalid get index '0' (on base: 'Array[Node]').

        Can you explain exact graze mechanics you want to implement?