0 votes

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 physicsprocess(delta):
global_position.y += speed * delta

func damage(amount: int):
health -= amount
if health <= 0:
if hassignal("bullet"):
damage(1)
queue
free()

-bullet code

extends Area2D

export (int) var bulletspeed = 300

var velocity = Vector2()

-signal bullet

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

func onVisibilityNotifier2Dscreenexited():
queue_free()

func onBulletbodyentered(body):
if body.isingroup("enemies"):
emitsignal("bullet")
queue
free()

in Engine by (46 points)

1 Answer

+1 vote
Best answer

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.

by (112 points)
selected by

It worked perfectly, thank you so much.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.