Variable death from the enemies

Godot Version

4.2.1

Question

I want to have different death animation from each enemy, but i’m beginner and that’s really hard to find in opensource.

extends Node
class_name HealthComponent

signal dead(killer)
signal damaged

@export var max_health: float

var current_health

func _ready():
	if(max_health == 0):
		printerr("Did not set health for"+ owner.name)
		return
	current_health = max_health

func damage(damage_amount: float, killer: Node = null):
	current_health = maxf(0.0, current_health - damage_amount)
	
	if (current_health == 0):
		dead.emit()

Doe`s that health componnent is ok for it?

  • How to make other components can find it?

    • Use metadata, such as owner.set_meta(&"Health", self).
  • How to show error with stacktrace?

    • push_error is better than print, print_rich and printerr. For example, push_error(message...).
  • Is that brackets in if statements is required?

    • No, you can use if cond: directly.
  • Other suggestions?

    • Assign a initial value to each variable.
1 Like

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