Getting hitboxes to affect healthbar

Godot Version

godot 4.3

Question

working on a fangame. I dont know how to get the hurtbox i have (from a series of YT tutorials) to affect the healthbar i have.

player code:

@onready var healthBar = $“camera pivot/Camera3D/healthBar”
@onready var LightBar = $“camera pivot/Camera3D/light”
@onready var StaminaBar = $“camera pivot/Camera3D/stamina”

func _ready() → void:
healthBar._init_health(Health)
LightBar._InitLight(light)
StaminaBar._InitStamina(stamina)

healthbar code:

var health = 0 : set = _set_health

func _init_health(_health):
health = _health
max_value = health
value = health

func _set_health(newHealth):
var prevHealth = health
health = min(max_value, newHealth)
value = health

hurtbox code:
extends Area3D
class_name Hurtbox

func _ready() → void:
connect(“area_entered”, self._on_area_entered)
collision_layer = 2
collision_mask = 2

func _on_area_entered(hitbox : Hitbox) → void:
### i don’t know if i should put anything here. it does print “collided” when a hitbox hits the hurtbox
print(“collided”)

You can either put the logic in the hurtbox script or use this “_on_area_entered”-method (if it only gets called when a hitbox enters).
Example:

func _on_area_entered(hitbox : Hitbox) → void:
    healthBar.decrease_health(hitbox.damage)
    print(“collided”)

It worked. Thank you!

1 Like