Custom signal seem like not connected?

Godot Version

Godot 4

Question

im trying to make an enemy take damage when the players sword area interacts with the enemy. My approach to doing this was using custom signals, but for some reason, it seems like it isnt connecting.

heres my relevant code

Player

signal enemy_hit

func _on_sword_area_body_entered(body):
	if body.is_in_group("enemy"):
		emit_signal("enemy_hit")

Enemy

@onready var player = $"../Player"
var health = 100.0

func _ready():
	player.connect("enemy_hit", handletakedamage)

func handletakedamage():
	health -= 20
	print(health)

Enemy is a child of player?

I would suggest a different design, when the sword hits the enemy call the take damage function directly instead of the signal.

You could make it better if you used OOP and had a base class that can take damage, e.g. class_name Hittable, then extend the Hittable class to make enemies and other items that could be destroyed.

Then when your weapon hits the body like:

If body is Hittable:
  body.takedamage()

enemy is not child of player, i am simply just grabbing the node from the scene tree

ill look into using classes, but i still understand why this doesnt work

Ah right it’s a sibling. Go up to the parent and get the player.

Okay, as far as your code goes it looks okay. So either your enemy is not in the enemy group. Or something stranger.

I would stick a break point on the weapon hit function and check the stack at that time.

func _on_sword_area_body_entered(body):
    breakpoint
	if body.is_in_group("enemy"):
		emit_signal("enemy_hit")

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