_on_area_entered works on other collision shapes when it shouldnt

Godot Version

v4.5.1

Question

on area entered code still runs even if the collision shape dosent have the class name that code should only run if decets that class name

class_name a_hurtbox
extends Area2D


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

func _on_area_entered(_area) -> void:
	if _area is a_hitbox:
		var parent = get_parent()
		if parent.has_method("take_damage"):
			parent.take_damage(10)
---------------------------------------------------------------------------------
class_name a_hitbox
extends Area2D

func _ready() -> void:
	collision_layer = 2
	collision_mask = 2
---------------------------------------------------------------------------------
( the colison shape it still detects )

extends CollisionShape2D
class_name collison

Isnt the point of your post, but you can set these in the inpector :smiley:

Anyways to the real point of your post!

I had a similar problem with this with my interactable objects. It was still detecting things it shouldn’t, like yours. I put them in a group instead, and put everything interactable on a separate collision layer. You could do the same for your hitboxes and hurtboxes.

Make your hitbox and hurtbox an instance, meaning theyd be seperate scenes. Save them into the inspector and then follow this code:

extends CollisionShape2D #your hurtbox


@onready var parent = Area2D #or the area your hurtbox is parented to, id recommend dragging it in but i wanted my instructions clear
@onready var hurtbox = "res://1.tscn" #whatever your scene for the hurtbox is titled


func _on_area_2d_area_entered(area: Area2D) -> void:
	if hurtbox.is_in_group("hello"): #replace the hello with whatever your group name is, make SURE if you are creating a group for these you toggle it to global. You can create groups in the inspector
		get_parent() #the area2d
#now after this im not sure if it works but if it doesnt tell me. I just dont have objects, the method, or the sprites to test it :) if it doesnt work can you share your sceen tree?
		if.parent_has_method("take damage"):
			parent.take_damage(10)
		elif:
			print("Does not have method take damage")

If this doesnt work tell me :slight_smile: i just started a new project to try and test this code, so i dont have a node that has take damage or sprites to test it, but i can troubleshoot if it doesnt work!

1 Like

thanks did some changes now code looks like this


@onready var parent = Node2D 
@onready var hurtbox = "res://a_hitbox"

func _on_area_entered(area: a_hitbox) -> void:
	if a_hitbox.is_in_group("HITBOX"): 
		get_parent() 
		if parent.has_method("take_damage"):
			parent.take_damage(10)
		else:
			print("Does not have method take damage")

tell me if i accidently broke something :sweat_smile:

now i get error with if a_hitbox.is_in_group(“HITBOX”): line “ Error at (15, 8): Cannot call non-static function “is_in_group()” on the class “a_hitbox” directly. Make an instance instead.” do you how can i fix it

and error with if parent has method Error at (17, 12): Function “parent_has_method()” not found in base self.

and forgot to say the hurtbox and hitbox are areas2d

oh i acidently delted one line

Its totally fixable :smiley:

Did you put the hurtbox and hitbox in the same scene as each other/something else?

no they are in 2 diffrent scenes sholud they be in same one?

no, they should be two different ones. :smiley:

Okay, found your problem! You made it a node2d as the root node, not an area2d. You can rightclick with your mouse on the area2d and then press ‘make root node’ to fix it, then you just gotta edit your code to reflect that change.

1 Like

i dont see “make root node” did you meant make scene root thats the closest thing i found :sweat_smile:

am i looking at right thing? ( am so sorry if am sking stupid questions :sweat_smile: )

How did you determine that?

i also had collision shape for geting hit if you walk into enemy and it also activated hurtbox code if i walked in the hurtbox collision shape

dont apologize, its okay!!

Yeah, its he right one, my fault

How do you know it activated it?

it activated take dammage function and printed the result

How do you know the call to take damage function came from a_hurtbox::_on_area_entered()?

i dont have other node that could have called that function

How do you know that?

Put a breakpoint in the take damage function and see how many times it gets called and where the calls are coming from by looking at the call stack.

am so confused i came back to the code i had on start and now it dosent call take damage function at all it did print when i put

func _on_area_entered(_area) -> void:
	print("!")

i dont know why is result diffrent than last time

That’s perfectly normal when debugging :slight_smile:

Put print statements and/or breakpoints all over the code to see where it’s going and what are the values of key variables. If anything looks/behaves different than expected - investigate more around that - more prints, more breakpoints.

@onready var parent = Node2D 
@onready var hurtbox = "res://a_hitbox"

func _on_area_entered(THE_HITBOX: a_hitbox) -> void:
	if THE_HITBOX.is_in_group("HITBOX") and THE_HITBOX is a_hitbox: 
		var local_parent = THE_HITBOX.get_parent() as a_hitbox
### you had if. here, dont put a dot after if
		if local_parent.has_method("take_damage"):
			local_parent.take_damage(10)
		else:
			print("Does not have method take damage")

Thats what i would do to the code, errors worth mentioning include the get_parent() not being assigned to anything in your original code, the dot after the if, and the use of the typename instead of the variable name.

really thanks the code i dont know why but game crashes at

		if local_parent.has_method("take_damage"):

error is “Invalid call. Nonexistent function ‘has_method’ in base ‘Nil’.”

do you know why it dosent work?

image