Resizing health bar separately for each enemy

This question is about a battle card game.

There are currently three enemy cards on screen, each with its own health and health bar, as well as five player cards.

All players are instantiated from the same scene (player_card) and all enemies are instantiated from the same scene (enemy_card). Player_card and enemy_card are different scenes.

In each turn, the player picks one of its cards and attacks one of the enemy cards. The player attack is handled properly. The code detects the player card that attacks, computes damage and deals damage to the chosen target enemy. So far, so good.

Then I try to update the enemy’s health bar by passing a signal to the enemy_card code. It works, except that, instead of updating the size of the health bar of the enemy that took the damage, all enemies’ health bars are updated simultaneously.

This is my code:

In GlobalSignals.gd (singleton)

signal update_enemy_healthbar

In Player_card.gd


(...) 

enemy_health_left  #this is computed from attack power - defense power*

#now compute the new size of the health bar, also correct
GlobalVars.enemy_health_bar_size.x = (enemy_health_left*GlobalVars.enemy_health_bar_size.x)/GlobalVars.enemy_active[6]
GlobalSignals.emit_signal ("update_enemy_healthbar")

In Enemy_card.gd

func _ready():

	GlobalSignals.connect("update_enemy_healthbar", update_enemy_healthbar_function)

func update_enemy_healthbar_function():
	$EnemyImage/EnemyHealthBar.set_size(GlobalVars.enemy_health_bar_size,$EnemyImage/EnemyHealthBar.size.y)

I believe the issue is with the very last function, which updates the health bar size of all instances of the enemy scene.

How would I go about updating only the health bar of the enemy that was attacked?

Thanks!

If you’d REALLY like to use a singleton for all signals, you can add the enemy info into the signal to specify the target.

signal update_enemy_healthbar(enemy_var)
#connect it to your function
GlobalSignals.update_enmemy_healtybar.connect(update_enemy_healthbar_function.bind(enemy_var))
#Now, in your function

func update_enmey_healthbar_function(enemy_var):
#and specify the enemy you want to change the healthbar function

I do have to say, this method is not recommended. If I were you, I would put the healthbar function into individual enemy.

Also, I believe you are using a custom rectangle shape for the health bar. I’d recommend you take a look at the ProgressBar Node for easier usage.

1 Like

I would avoid a signal for this since you need access to the enemy anyways. If enemy_active[6] is the attacked enemy, then call their update function directly.

GlobalVars.enemy_health_bar_size.x = (enemy_health_left*GlobalVars.enemy_health_bar_size.x)/GlobalVars.enemy_active[6]
GlobalVars.enemy_active[6].update_enemy_healthbar_function()

Both work, thanks!

I ended up removing the signal because it makes my life easier.