Enemy won't take damage

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

I’m just learning how to code so please excuse any blatant errors I may have made.
I want the enemy to take damage and die after it’s health reaches 0.
The bullet seems to function as intended, destroying itself after making contact with an enemy but the enemy doesn’t take any damage. The enemy is a kinematic body 2d and not an area 2d like the bullet because I want the enemy to directly interact with the player.

-enemy code

extends KinematicBody2D

func _ready():
add_to_group(“enemies”)

export (int) var speed = 50
export (int) var health = 1

signal enemy

func _physics_process(delta):
global_position.y += speed * delta

func damage(amount: int):
health -= amount
if health <= 0:
if has_signal(“bullet”):
damage(1)
queue_free()

-bullet code

extends Area2D

export (int) var bulletspeed = 300

var velocity = Vector2()

-signal bullet

func _physics_process(delta):
global_position.y -= bulletspeed * delta

func _on_VisibilityNotifier2D_screen_exited():
queue_free()

func _on_Bullet_body_entered(body):
if body.is_in_group(“enemies”):
emit_signal(“bullet”)
queue_free()

:bust_in_silhouette: Reply From: betauer

It looks like nobody is calling to the enemy damage funcion, you are just emiting a signal “bullet” in your bullet class, but the enemy is not connected to them.

Try to call to body.damage(1) instead of emit_signal("bullet"). In your enemy code, remove the has_singal("bullet") condition because it will be always false (has_signal returns true if the enemy has this signal, which I guess is not the case). Remove also the damage(1) in your func damage() because it will end in a infinite loop and your game will crash.

It worked perfectly, thank you so much.

wolvertox | 2022-11-15 16:40