Color rect size not changing

Godot Version



<Im using color rect for my boss heal bar. Whenever the boss takes damages, id like to make color rect size.x - damage amount. However, the size doesnt change.

func take_damage(amount: int) -> void:
	print("take damage")
	print("DMG:", amount)
	health_bar.size.x = 424 - amount
	health = max(0, health - amount)
	if health == 0:
		health_bar.size.x = 0

You’re setting it to what I think is the max health of the boss minus the damage amount each time by using “health_bar.size.x = 424 - amount”, rather than decrementing on each hit. So it’s only ever going to be 424 - the last damage hit. So if you hit the boss with 20 damage every time. It will always result to the size 404.

How I would do it:

var max_health : int = 424
var current_health : int = 0

func _ready() -> void:
    current_health = max_health
    health_bar.size.x = max_health

func take_damage(amount : int) -> void:
    print("take damage")
    print("DMG: ", amount)
    current_health -= amount
    health_bar.size.x = current_health
    if current_health <= 0: # check for less than or equal just to catch potential errors since you're not clamping it
		health_bar.size.x = 0

Additionally. I don’t see the reason for the "health = max(0, health - amount). Max give you the biggest number of the given values so this does nothing. You are essentially doing “health = health - amount”. I might be wrong but are you trying to set a minimum an maximum value that health is allowed? If so, you use the “clamp” function for that.

clampi(current_health, 0, max_health) # use clampi() because it's an int