Hitboxes and hurtboxes arent working and there arent any errors

Godot Version

4.5.1

Question

been trying to whrite hitboxes & hurtboxes but they arent doing anyhting and there arent any errors

class_name a_hurtbox
extends Area2D

func _ready() -> void:
	collision_layer = 64
	collision_layer = 64
	area_entered.connect(_on_area_entered)

func _on_area_entered(area) -> void:
	if area is a_hitbox:
		get_parent().take_damage()


-------------------------------------------------------------------------------

class_name a_hitbox
extends Area2D

func _ready() -> void:
	collision_layer = 64
	collision_layer = 64

-------------------------------------------------------------------------------
( paremt of hurtboxes )

extends Node2D

@export var health1 = 40


func take_damage():
	health1 -= 10
	print(health1)

	if health1 <= 0:
		print("enemy died")

does anyone see waht might be the issue

Set the collision_mask of the hurtbox to 64.

3 Likes

You’re setting the collision layer twice instead of the mask. Also, I think it would be helpful to use names for the physics layers instead of numbers. (64 means the 7th physics layer)

2 Likes

typing that 2 times was just a mistake i just didint save that version of code and typed that too fast

its
collision_layer = 64
collision_mask = 64
in the code but still it dosent work

Put a breakpoint or print statement in _on_area_entered and to check if it ever gets called.

Turn on visible collision shapes and make sure that the hit box and hurt box overlap when you expect them to.

4 Likes

this is not a good way to do it. I’m not sure, but I think area will be Area2D, never a_hitbox (also, class_names should be in PascalCase as per the gdscript style)
Instead, cast area and check if it’s null:


var a_hb : a_hitbox = area as a_hitbox
if a_hb:
	get_parent().take_damage()

collision_layer is not the same as collision_mask. collision_layer is the layer the object belongs to, collision_mask is the objects it can collide with.
also, no need to assign it twice.

1 Like

Just tested it, the custom area2d-derived class will count as both an Area2D and the custom class as well (even if you type hint the method to area: Area2D). So I do not think that’s the issue.

3 Likes
class_name a_hurtbox
extends Area2D

func _ready() → void:
collision_layer = 64
collision_mask = 64
area_entered.connect(_on_area_entered)

func _on_area_entered(area) → void:        <---------
print(“A”)

i think the func _on_area_entered(area) → void: (area) was the issue and after i just deleted its just () now _on_area_entered() ) it actually does something and gives error in debugger :

"emit_signalp: Error calling from signal ‘area_entered’ to callabke ‘Area2d(hurtbox.gd)::_on_area_entered’: Method expected 0 arugemnt(s), but called with 1.

how can i fix that?

The area_entered signal always passes the area that entered as argument. Because of that, the signal needs to be connected to a function that can receive this argument. The error happens because you removed the (area) parameter, so you need to put that back in.

That said, according to the error, the signal was emitted and did attempt to call the function. Did you test this version before removing (area)? Was it printing “A”?

1 Like