area collision confused on body exited signal

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

I have a tank project going and have come across a problem with area collision for my turret aiming. what will happen is the turret aims at targets as they enter the area, if I drive two tanks into the area , then drive one tank out the enemy tank will lose the remaining tank even if it was the tank it was targeting. I have on body exited set to change target to null , but even if I change that to just pass the tanks just keep target lock after they exit the area leaving the enemy or player tank targeting an exited body while other bodies are still in the area.

can I check the area for remaining tanks? or maybe store targets as they enter the area would work?

This is a very confusing explanation. It is much better and much simpler to just share your code. Please share your tank controller script as well as a screenshot of your scene tree.

Bernard Cloutier | 2020-09-29 15:59

Thank you Bernard Cloutier for the reply , sorry my question is confusing , the turret has an area 2d with a collision shape to detect a tank in range and target the intruding body , it was adapted from KidsCanCode tank tutorial. I just needed to expand on the idea so I added an array to store the intruding bodies and then remove them in the on body exited function and targeting is not set to null until the array is empty, it seems to work good , I used “array.pushback(body)” so the turret doesn’t re-target for each unit that enters its field.

ArthurER | 2020-09-29 16:50

Please copy and paste your code and add screenshots of your project’s scene tree. It is very hard to diagnose the source of the problem without them.

Bernard Cloutier | 2020-09-29 19:58

:bust_in_silhouette: Reply From: ArthurER

Thank you Bernard Cloutier , I found a solution to my problem. I will pass on the image post but here is the solution I used.

var in_range = []

func _on_DetectRadius_body_entered(body):
  in_range.push_back(body)
  target = in_range[0]

func _on_DetectRadius_body_exited(body):
  var x = in_range.find(body)
  if x > -1:
	  in_range.remove(x)
  if in_range.size() == 0:
	  target = self
  else:
	  target = in_range.pop_front()

pasting code here does not seem to like tabs so indents are off.