Godot body_entered() function not triggering

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MartinToilet

Hey guys, I’ve been trying to create a hitbox and a hurtbox program.

My design logic is that every hitbox is a combination of one or more than one Area2D node. Also, the Area2D node can be freely added.
This is my Hitbox code:

extends Node

onready var hurtboxes: Array = []

#the message to send to hurtbox
var message: Dictionary = {}

func _ready() -> void:
	add_to_group("hitbox")
	#looping through all the Area2D(hitboxes) node it possesses
	for node in self.get_children():
		if node is Area2D:
			var hb: Area2D = node
			hurtboxes.append(hb)
			hb.monitoring = true
            #connecting body_entered signal
			hb.connect("body_entered", self, "_on_hurtbox_entered")


func _process(delta: float) -> void:
	pass

			
func _on_hurtbox_entered(body: Area2D) -> void:
	#this function wasn't triggered as it should
	if body.get_parent().is_in_group("hurtbox"):
		body.connect("hit", self, "get_hit")
		

func get_hit(dict: Dictionary) -> void:
	#hit logic
	pass

and this is the Hurtbox code:

extends Node

onready var hurtboxes: Array = []

#storing the message from the hitbox
var recieved_message: Array = []

func _ready() -> void:
	add_to_group("hurtbox")
	#looping through all the Area2D(hurtboxes) node it possesses
	for node in self.get_children():
		if node is Area2D:
			var hb: Area2D = node
			hurtboxes.append(hb)
			hb.monitoring = true
			#connecting the hurtbox signal
			hb.connect("body_entered", self, "_on_hitbox_entered")


func _process(delta: float) -> void:
	pass

			
func _on_hitbox_entered(body: Area2D) -> void:
	#getting hit
	if body.get_parent().is_in_group("hitbox"):
		body.connect("hit", self, "get_hit")
		


func get_hit(dict: Dictionary) -> void:
		recieved_message.append(dict)

So if any of the hitbox’s Area2D child node detects a hutbox’s Area2D child node, it should trigger the _on_hitbox_entered() function. But the function was never triggered when I tested it.

Any help would be appreciated!!!

I suppose you tested(with some print messages for example), and the actions in “get_hid” not triggered. I think you need to define the signal “hit” which you use to connect the “get_hit” method. Also if the problem is in connect you’ ll have some error messages in “debug” tab on editor when you run your project, which may can help to understand what is wrong.

dancaer69 | 2021-05-16 07:07

I’ve tried printing some message in the _on_hurtbox_entered() function and it didn’t print, so obviously, the function was not triggered.

MartinToilet | 2021-05-16 08:05

So the “on_entered()” functions not triggered. Do you have any other control nodes in the scene? Then maybe some other control node steal the focus. Try(if this is the case) to set the Filter in Mouse property of that node(or nodes) to “Ignore”.

dancaer69 | 2021-05-16 09:48

My Hitbox scene is a Position2D node attached with a script and also the same as the hurtbox, also, I got a Player scene with a Hitbox node in it and I also added an Area2D node a child of my Player’s Hitbox node. I do this because that I can modify the shape of my hitbox determine on the attack I use. I’m new to Godot, so I don’t know if my description makes sense or not but for me I see that I got 3 scenes: Level scene(contains a Player), Player scene(contains Hitbox node), and Hitbox(or hurtbox) scene. And may I ask that what does it have to do with Mouse Property and what is Mouse Property? Pls forgive me for asking this stupid question.

MartinToilet | 2021-05-17 05:07

From your description, seems that you haven’t any control node. Control nodes(button, textureButton, textureRect for example) have a mouse property.
Control — Godot Engine (stable) documentation in English

dancaer69 | 2021-05-17 05:41

So, now I’ve changed my Level scene from a Node2D to a Control type and I also set the mouse property to ignore in the inspector tab. The thing is that the event is still not getting triggered.

MartinToilet | 2021-05-17 06:07

No, you misunderstood. You don’t need to change to a control node. I mentioned control nodes because they can cause area2d events to not triggered if exist in the scene, so you need to filter the mouse property. If you haven’t any, then the problem caused by something else.
Did you check the debugger’ s errors tab when you run the project? Maybe there are some errors there which can help. And also there is “pickable” property in area2d which needs to be enabled.

dancaer69 | 2021-05-17 06:23

I just managed to trigger the event by adding a bullet to test. By the way, thanks for taking your time to answer me and so far I’ve learned some new things.

MartinToilet | 2021-05-17 08:25

:bust_in_silhouette: Reply From: MartinToilet

I just managed to trigger the event after creating another bullet class with hitbox node in it. Although I still don’t understand why it didn’t work when I test it with the player but now it works.