HitBox, HurtBox

Godot Version

Godot 4.6.2

Question

Hello how can I fix issue with on_area_entered: I use HitBox of a sword in animation and when it enters HurtBox it deals correctly dmg. But if you are fast enough and get the sword out of the area before attack animation is over and back in it deals damage again. Any idea how to fix it?

There is code:

extends Area3D
class_name HitBox

func _ready() -> void:
	connect("area_entered", Callable(self, "_on_area_entered"))

func _on_area_entered(hurtbox: Area3D) -> void:
	if hurtbox is HurtBox:
		if owner.has_method("take_damage") and hurtbox.is_in_group("weapons"):
			owner.take_damage(hurtbox.get_damage())

extends Area3D
class_name HurtBox

@export var damage := 5

func get_damage() -> int:
	return damage

Keep track of which Hitboxes have already been hurt in this attack in an Array and don’t hurt them more than once. Clear the Array after the attack is completed.

I am new to all of this. Can you expand your answer a bit more?

Do you know how an Array works? If not, I suggest to check these out first:

Let me know if you still have issues figuring this out afterwards and what you have tried.

I was a bit lost on arrays so I used Dictionary. And managed to get it working. Is it not optimal in a long run? Should I implement arrays instead of Dictionary?

No, Dictionary is a fine choice as well.

dictionaries are ok, tho if you worry or want to try out arrays, they’re lists of objects where you have numbers corresponding to each value, and you can use array.has or array.find to find specific value, for instance if hitbox you’ve hitted is on the list or not