Progressbar for player and enemies at HUD?

Godot Version

4,4

Question

I've created a ProgressBar to show at HUD the progress life of player and enemies instead of to add on each of them, my purpose is that it works as Final Fight for example showing on left side the progressbar of player and right side progressbar of enemies. The progressbar tests if the health of player or enemy is less than middle or minimus and it'll going to changing the color of Progress as enemie or player receive hit, and here is the problem. The progressbar of player and enemies is changing color at same time. For example, if player receive hit and it changing color the progressbar of enemy is chaging color too. How could I fix it ?

ProgressBar

class_name BaseHealthLifeProgressBar extends Node2D

#VAR
var _max:int = 50
var _middle:int = 25
var _min:int = 15
var _step:int = 1
var _total_damage:int = 0
var _total_health:int = 0

@onready var progress_bar: ProgressBar = $ProgressBar
@onready var sprite_face: Sprite2D = $SpriteFace
@onready var label_name: Label = $LabelName

	
#set de default values by character 
func progressbar_set_default_values(_vmax:int = 50, _vmiddle:int = 25, _vstep:int = 5) -> void:	
	progress_bar.max_value = _vmax	
	progress_bar.value = _vmax
	progress_bar.step = _vstep
	_total_health = _vmax	
	
#progressbar change value
func progressbar_changevalue_damage(damage:int = 1) -> void:
	_total_damage += damage	
	progress_bar.value = _total_health - _total_damage	
	if(progress_bar.value <= _middle and progress_bar.value > _min):
		progressbar_change_color(Color.YELLOW)
	elif(progress_bar.value <= _min):
		progressbar_change_color(Color.RED)
	
#progressbar loading image at Sprite2D
func progressbar_loading_img(img_name:String) -> void:
	var img_path:String = "res://assets/faces/%s.png" % img_name
	var IMG = load(img_path)
	sprite_face.texture = IMG
	
#progressbar set name at Label
func progressbar_set_person_name(name:String, picture:String) -> void:
	label_name.text = name
	progressbar_loading_img(picture)
	
#return %
func progressbar_get_percentage() -> void:
	pass
	#return int((value - min) / (max - min)) * 100
	#var _percent:float = int((value - min) / (max - min)) * 100
	
#progressbar set full when get item full health
func progressbar_reset_full_life(life_health:int) -> void:
	progress_bar.value = life_health
	
#progressbar change color 
func progressbar_change_color(color:Color) -> void:
	progress_bar.get("theme_override_styles/fill").bg_color = color

HUD

class_name HUD extends Control

@onready var hl_progress_bar_player: BaseHealthLifeProgressBar = $MC_TOP/MarginContainer/HBoxContainer/HL_ProgressBar_Player
@onready var hl_progress_bar_enemies: BaseHealthLifeProgressBar = $MC_TOP/MarginContainer/HBoxContainer/HL_ProgressBar_Enemies

func _ready() -> void:
	#player progressbar
	SignalManager.set_player_values_healthbar.connect(set_player_values_healthbar)
	SignalManager.set_player_name_picture_healthbar.connect(set_player_name_picture_healthbar)	
	SignalManager.on_player_hit_update_healthbar.connect(on_player_hit_update_healthbar)
	#enemy progressbar
	SignalManager.set_enemy_values_healthbar.connect(set_enemy_values_healthbar)
	SignalManager.set_enemy_name_picture_healthbar.connect(set_enemy_name_picture_healthbar)	
	SignalManager.on_enemy_hit_update_healthbar.connect(on_enemy_hit_update_healthbar)
	
#player healthbar
func set_player_values_healthbar(_vmax:int, _vmiddle:int, _vstep:int):
	hl_progress_bar_player.progressbar_set_default_values(_vmax, _vmiddle, _vstep)
	
#player set name and picture
func set_player_name_picture_healthbar(name:String, picture:String):
	hl_progress_bar_player.progressbar_set_person_name(name, picture)
	
#player change progressbar value damage
func on_player_hit_update_healthbar(damage:int):
	hl_progress_bar_player.progressbar_changevalue_damage(damage)
	
#enemy healthbar
func set_enemy_values_healthbar(_vmax:int, _vmiddle:int, _vstep:int):
	hl_progress_bar_enemies.progressbar_set_default_values(_vmax, _vmiddle, _vstep)
	
#enemy set name and picture
func set_enemy_name_picture_healthbar(name:String, picture:String):
	hl_progress_bar_enemies.progressbar_set_person_name(name, picture)
	
#enemy change progressbar value damage
func on_enemy_hit_update_healthbar(damage:int):
	hl_progress_bar_enemies.progressbar_changevalue_damage(damage)

All your progress bars are using the same resource. Resources are shared by default. When you change the resource, all the progress bars change. You need to make the resource unique to both progress bars.

Edit: Or maybe simpler and better: Use white background for the progress bar theme and change the color using modulate or self_modulate properties. This would also avoid resource duplication, which makes it easier to maintain, uses less memory and… Is just all around better.

1 Like

Thanks a lot. It works well