Health - cluttered-code/godot-health-hitbox-hurtbox GitHub Wiki
inherits: Node
Tracks the health of an entity.
Description
Tracks the health of an entity and emits various signals when changes to current occur through damage() or heal() function calls.
Add this component as a direct child of an entity or by setting the exported variable. The entity will be sent with each signal to associate an entity with the health component that was affected.
The component can be used standalone or paired with a HurtBox2D/HurtBox3D to automatically invoke damage() and heal() when collisions are detected by HitBox2D/HitBox3D or HitScan2D/HitScan3D components.
Tutorials
Constants
type | name | value |
---|---|---|
int | DEFAULT_MAX | 100 |
Properties
type | name | default |
---|---|---|
int | current | DEFAULT_MAX |
int | max | DEFAULT_MAX |
Node | entity | owner |
bool | damageable | true |
bool | healable | true |
bool | killable | true |
bool | revivable | true |
Methods
return | function |
---|---|
void | damage() |
void | heal() |
float | percent() |
void | kill() |
void | fill_health() |
bool | is_dead() |
bool | is_alive() |
bool | is_full() |
Signals
damaged
damaged(entity: Node, amount: int, applied: int, multiplier: float)
Emitted after damage is applied to current health.
- entity is the Node in the entity property.
- amount is the amount specified when
damage()
is called. - applied is the amount current was actually reduced by. Only different when
amount
would have made current negative. - multiplier is the value amount is multiplied by to determine how much to apply.
died
died(entity: Node)
Emitted after damage is applied to current health and death has occured.
- entity is the Node in the entity property.
healed
healed(entity: Node, amount: int, applied: int, multiplier: float)
Emitted after healing is applied to current health.
- entity is the Node in the entity property.
- amount is the amount specified when
heal()
is called. - applied is the amount current was actually increased by. Only different when
amount
would have made current greater than max. - multiplier is the value amount is multiplied by to determine how much to apply.
revived
revived(entity: Node)
Emitted after healing is applied to current health when entity was previously dead.
- entity is the Node in the entity property.
already dead
already_dead(entity: Node)
Emitted when attempting to damage an entity that is already dead.
- entity is the Node in the entity property.
already full
already_full(entity: Node)
Emitted when attempting to heal an entity that is already full.
- entity is the Node in the entity property.
first hit
first_hit(entity: Node)
Emitted when damage was applied to an entity that had full health.
- entity is the Node in the entity property.
full
full(entity: Node)
Emitted when healing was applied to an entity that now has full health.
- entity is the Node in the entity property.
not damageable
not_damageable(entity: Node)
Emitted when attempting to damage to an entity and damageable is set to false
.
- entity is the Node in the entity property.
not healable
not_healable(entity: Node)
Emitted when attempting to heal an entity and healable is set to false
.
- entity is the Node in the entity property.
not killable
not_killable(entity: Node)
Emitted when attempting to damage an entity that would kill them and killable is set to false
.
- entity is the Node in the entity property.
not revivable
not_revivable(entity: Node)
Emitted when attempting to heal and entity that is dead and revivable is set to false
.
- entity is the Node in the entity property.
Enumerations
enum Action
-
This action will apply damage and reduce Action DAMAGE = 0current health.
-
This action will apply healing and increase Action HEAL = 1current health.
Property Descriptions
current
@export var current: int = DEFAULT_MAX
The current amount of health. Value is clamped in setter between [0, max]
max
@export var max: int = MAX_DEFAULT
The maximum amount of health. Value cannot be less than 1, setter will update current to remain in the bound of a new max.
entity
@export var entity: Node = owner
The Node
this component is associated with. All signals will pass this entity so listeners will know which entity's health was affected. If this health component isn't a direct child of the entity it is associated with this variable can be set to any desired Node.
damageable
@export var damageable: bool = true
Damage can be applied when this is true
.
healable
@export var healable: bool = true
Healing can be applied when this is true
.
killable
@export var killable: bool = true
entity can be killed when this is true
.
revivable
@export var revivable: bool = true
entity can be revived when this is true
.
Method Descriptions
damage
damage(amount: int, multiplier: float = 1.0) -> void
Decrement current if applicable and emit signals that apply.
heal
heal(amount: int, multiplier: float = 1.0) -> void
Increment current if applicable and emit signals that apply.
percent
percent() -> float
Returns the calculated percent current divided by max. Perfect for updating progress bars.
kill
kill() -> void
Decrement current to 0 if and emit signals that apply.
fill_health
fill_health() -> void
Increment current to max and emit signals that apply.
is_dead
is_dead() -> bool
Returns true
when current is 0 and killable is true
.
is_alive
is_alive() -> bool
Returns true
when is_dead() is false
.
is_full
is_full() -> bool