How to detect multiple enemies in area2d and target it all at once

Godot Version

4.3

Question

Hi i have this skill that fall down but i only manage to strike 1 enemy at a time but when the enemy die i need to get out or the range and enter again to detect another enemy. if it possible i want to detect or store multiple enemies position at once or even an enemy die i can still locate another enemy position without even exiting the skill range and enter again. Thanks so much in advance!!!

You can store all the objects inside a array when they enter and remove them when they leave:

func _on_body_entered(body):
    targets.append(body)

func _on_body_exited(body):
    targets.erase(body)
2 Likes

hi how can i access the object position inside the array ? thanks in advance !

for i in targets:
      print(targets[i].global_position)
1 Like

thanks i will try this !

it give me this error sorry new to godot

thanks for the help

it has to be this:

for target in targets:
      print(target.global_position)

or this:

for i in len(targets):
      print(targets[i].global_position)
1 Like

it works! thanks so much ive been had a hard time to search for this prob cause i cant access the object position inside of the array if im correct idk how i will study this Thanks so much !!! both of you!!! :smiling_face_with_three_hearts:

i have a question why target[i].global_position not work or target[i]

that depends on the way you do the for-loop.
If you do for i in targets:, ā€˜iā€™ will take be values that are inside the targets-array. And when you dofor i in len(targets): ā€˜iā€™ will be 0, 1, ā€¦ , len(targets) and you can only access an element inside an array with a number, so the second method

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.