Trouble with enemy groups and detecting them

Godot 4.2.2

New to programming in Godot. I am working on a project that has multiple enemy characters.

This is my goal:
multiple enemies in a group called “enemies”
if detection area of player in entered, check “enemies” group(array)
if the entity is in the array aim at and shoot

I am having trouble with getting this to work. I was able to have the player character shoot at a single enemy when the var for enemy is specified to a specific enemy. But I am having trouble with having when it comes to detecting multiple/various different enemies.

This is my aiming function:
func _aim():
if not is_instance_valid(enemy):
return
else:
ray_cast.target_position = to_local(enemy.position).normalized() * max_length

This collision check works for a specified enemy:
func _check_enemy_collision():
if ray_cast.get_collider() == enemy and timer.is_stopped():
timer.start()
elif ray_cast.get_collider() != enemy and not timer.is_stopped():
timer.stop()

If more context or code is needed I’m happy to provide!

This code here seems to only be searching fo an enemy. It isn’t checking if there are multiples.

However, it is hard to know exactly what to suggest without knowing how you want to interact with multiple enemies.

Do you want to check all enemies and find the closest?

Do you want to target all enemies within a certain range?

Also, you mentioned area entered signal, is that connected to _aim() or elsewhere?

You could make an @onready var enemies = get_tree().get_nodes_in_group(“enemies”)

Now you can use a for loop.

Var targets : Array

For enemy in enemies:
#Do what you want as far as targeting, or figuring out if they are in range. 
If enemy.global_position.distance_to(player.global_position) < 100:
targets.append(enemy)

This is just an example, it depends on exactly how you want to interact with the multiple enemies.

Okay, reading back through I might understand slightly better.

@onready var enemies = get_tree().get_nodes_in_group("enemies")

Var enemy

func _aim():
If not instance_is_valid(enemy):
return
else:
ray_cast.target_position = to_local(enemy.position).normalized() * max_length

func _check_enemy_collision():
For unit in enemies:
if ray_cast.get_collider() == unit and timer.is_stopped():
Enemy = unit
timer.start()
elif ray_cast.get_collider() != unit and not timer.is_stopped():
Enemy = unit
timer.stop()

I don’t know how the rest of your code is setup, but that should allow you to detect enemies from the group if your other code is setup well.

If aim is called before check enemy collisions youll get an error.

1 Like

Thanks a lot! I’m gonna try these and get back to you if they work properly. I really appreciate the help.
The goal is the player character looks at the closest enemy first within the detection area. I couldn’t figure out how to get it to look through the array of enemies. I think this is what I was looking for

I just tested it out and it works beautifully!! I can’t thank you enough. I am a novice with coding but I am enjoying the journey

Yeah, no problem man. I have learned so much in the last year because I’ve been helped out.

For loops are great whenever you want to iterate over multiple things.

The form is: for (a name of your choosing) in (something that has multiple things in it, like an array a dictionary or just a regular number):

1 Like

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