Connect function in code not working

Godot Version

Godot 4.2.1

Question

I am trying to connect a signal to a function. I pretty sure that all the variables are properly referenced(since they are exported, and I set them to the node). I have gotten over this by connecting it in the editor, but I still want to make sure what I got wrong, because I know I can’t always connect it in editor. Here’s the code:

extends Node2D
class_name DeathHandler2D
@export var node_to_kill:PhysicsBody2D
@export var hitbox:Hitbox2D
@export_category("Optional")
@export var animation_player:AnimationPlayer
@export var my_collision_shape:CollisionPolygon2D:
	set(value):
		if not my_collision_polygon:
			my_collision_shape=value
@export var my_collision_polygon:CollisionPolygon2D:
	set(value):
		if not my_collision_shape:
			my_collision_polygon=value
# Called when the node enters the scene tree for the first time.
func ready():
	if not hitbox.area_entered.is_connected(check_area):
		hitbox.area_entered.connect(check_area)
func check_area(area:Area2D):
	if area is KillBox2D:
		if area.get_damage():
			var damage:int=area.get_damage()
			hurt(damage)
		hurt()
func hurt(damage:int=0):
	if node_to_kill.has_method(&"hurt"):
		node_to_kill.hurt(damage)
	if can_one_shot(node_to_kill,damage):
		if node_to_kill.has_method(&"die"):
			node_to_kill.die()
	else:
		node_to_kill.queue_free()

I know it is not any other problem, especially because the only thing that solved this problem was the connecting in editor.

Try somthing like this


hitbox.connect("area_entered",check_area)

Also make sure hitbox has monitoring enabled.

I have tried that way before too. Doesn’t work.

Can you use “body_entered” instead?


	$Area2D.connect("body_entered", (func(body):
				print("here"))
			)

No, I’m using an custom class that inherits from area2ds

I don’t think there’s enough information to solve the problem. Have you tried using breakpoints to see if the line connecting the signal is executed at all, and how exactly do you know the signal isn’t connected?

1 Like

I tried this, and the ready function just seems to not be calling, so I guess its a entirely different error now

I put it in enter_tree and it works now. Why?
EDIT: I just realized I misspelled ready. OMG I’m stupid as h*ck.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.