Isnt the point of your post, but you can set these in the inpector
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 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!
@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
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
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.
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.