0 votes

I'm trying to make a bullet deal damage to an enemy on collision and check if its health is less than 1, and if so, initiate its deathInitiate()function so the enemy dies and is removed in the same frame.
I couldn't figure out how to do this, though I thought of just bringing that function to the bullet itself and applying it to the enemy (but I really don't wanna do this and feel like its a last resort)

in Engine by (17 points)

1 Answer

+1 vote
Best answer

In the enemy script add the function to take damage:

func take_damage(damage_amount):
    damage -= damage_amount
    if damage < 1:

Declare the initiate death function in the same enemy script:

func initiate_death():

Then in bullet script, declare bullet damage amount and a hit function:

var damage_amount = 10
func hit(body):
    if body.has_method("take_damage"):

Also make sure to connect body_entered signal to the hit function in _ready() function.

func _ready():
    connect("body_entered", self, "hit")

I have assumed you are using godot 3 since you didn't specify which version you are using. If you are using godot 4 and have problems converting the code just comment on this answer.

by (2,017 points)
selected by

I'm not home yet to try, but is the damage dealt and enemy destroyed in the same frame?

queue_free() will destroy the node when ready. It may be this frame or the next but that's a very small time difference that can't even be noticed.
Why is it so important anyway?

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.