I'm trying to cycle through the mobs that are within range

Godot Version

4.8.dev5

Question

I’m trying to have the code cycle through mobs that enter the range of the orb. The range of the orb is shown in the screenshot below:

However, I keep getting an error. The nodes are arraigned in the screenshot below:

The code of Line2D is below:

extends Line2D

var firing_status: bool = false
var shot_duration: float = 0.05

var shot_delay: float = 0.3

@onreadyonready var shot_sound: AudioStreamPlayer = $“../../../Shot_@onreadyound”
@onready var balance: Label = $“../../../Main_CanvasLayer/VBoxContainer/HBoxContainer/Balance”

func _process(_delta: float):
#Updates the balance in the label
balance.text = str(Globals.balance)
for mobs in Globals.mobs_in_range:
shooting_mob()

func _on_orb_area_shape_entered(_area_rid: RID, area: Area2D, _area_shape_index: int, _local_shape_index: int):
Globals.mobs_in_range.append(area) #<-----I’m not sure what the correct variable here is.

func shooting_mob():
add_point(Vector2(0,0))
add_point(to_local(Globals.mobs_in_range[0].position))
#add_point(to_local(Globals.mobs_array[0].position))
await get_tree().create_timer(shot_duration).timeout
clear_points()
await get_tree().create_timer(shot_delay).timeout
Globals.shoot_counter -= 1
print(str(Globals.shoot_counter) + " shots left")
Globals.mobs_in_range[0].when_attacked() #<------Nonexistent function ‘when_attacked’ in base ‘Area2D’. error happens here
Globals.mobs_in_range.erase(Globals.mobs_in_range[0])
shot_sound.play()

The screenshot below are the nodes of the mob scene:

Code of the mob scene below:

extends @onreadyaracterBody2D

@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D

var SPEED: int
const DIRECTION = -1
var hit_points: int = 1

func _ready():
$AnimatedSprite2D.play()
SPEED = Globals.mob_speed

func _physics_process(_delta: float):
velocity.x = SPEED * DIRECTION
if velocity.length() > 0:
$AnimatedSprite2D.play(“run”)
move_and_slide()

func _on_area_2d_body_entered(_body: Node2D):
SPEED = 0
attacking_mode()

func attacking_mode():
while Globals.wizard_tower_health > 0:
$AnimatedSprite2D.play(“attack”)
await $AnimatedSprite2D.animation_finished
Globals.attacked_signal.emit()

func when_attacked():
hit_points -= 1
if hit_points < 1:
SPEED = 0
$AnimatedSprite2D.play(“death”)
Globals.balance += 1
await $AnimatedSprite2D.animation_finished
call_deferred(“queue_free”)

Did some more testing based on feedback from a Discord, the updated code that worked is shown below:
func _on_orb_area_shape_entered(_area_rid: RID, area: Area2D, _area_shape_index: int, _local_shape_index: int):
var mob = area.get_parent()
if mob.has_method(“when_attacked”):
Globals.mobs_in_range.append(mob)
print(Globals.mobs_in_range)

func shooting_mob():
var targeted_mob = Globals.mobs_in_range[0]
add_point(Vector2(0,0))
add_point(to_local(targeted_mob.position))
await get_tree().create_timer(shot_duration).timeout
clear_points()
await get_tree().create_timer(shot_delay).timeout
Globals.shoot_counter -= 1
print(str(Globals.shoot_counter) + " shots left")
targeted_mob.when_attacked()
Globals.mobs_in_range.erase(targeted_mob)
shot_sound.play()

You can get the owner of the area (the scene’s root node) and check for the type you defined to that root’s script via class_name (for example MyMob). Then if the owner has the right type you add it to an Array of MyMobs.