#[SOLVED] Health bar replication change for all Characters

5 messages · Page 1 of 1 (latest)

hardy ledge
#

In Aura Project, we add the HealthBar component (Healthbar above the head) to the AuraEnemy, but I want to add it to both Enemy and Aura Characters.
So instead I have placed the component on CharacterBase (Mine is called HeroCharacterBase instead of AuraCharacterBase), and initialized it in the BeginPlay:

void AHeroCharacterBase::BeginPlay()
{
  Super::BeginPlay();

  UHeroUserWidget* HeroUserWidget = Cast<UHeroUserWidget>(HealthBar->GetUserWidgetObject());
  if(HeroUserWidget)
  {
    // Sending data to health bar above character, to be seen by other players
    HeroUserWidget->SetWidgetController(this);
    const UHeroAttributeSet* HeroAttributesSet = Cast<UHeroAttributeSet>(AttributeSet);
    if(HeroAttributesSet)
    {
      AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(HeroAttributesSet->GetHealthAttribute()).AddLambda(
      [this](const FOnAttributeChangeData& Data)
      {
        OnHealthChangedDelegate.Broadcast(Data.NewValue);
      });
      
      AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(HeroAttributesSet->GetMaxHealthAttribute()).AddLambda(
      [this](const FOnAttributeChangeData& Data)
      {
        OnMaxHealthChangedDelegate.Broadcast(Data.NewValue);
      });
      
      OnHealthChangedDelegate.Broadcast(HeroAttributesSet->GetHealth());
      OnMaxHealthChangedDelegate.Broadcast(HeroAttributesSet->GetMaxHealth());    
     }
  }
}

In the WBPHealthbar I then cast to the base character instead of Enemy, see attached image.

This works when attacking a EnemyCharacter, both Server and Client gets a updated healthbar when health changes.

But when a AuraCharacter attacks each other (both on Server and Client) their own health bar updates but it does not replicate to each other.
Any tips on how to fix this would be much appriciated, thanks in advance.

hardy ledge
#

For anyone that is intrested, I found a solution that seems to be working.
Moved this into a public function I called "void InitListenAttributeChange();" on the CharacterBase and called the function in BeginPlay.

  UHeroUserWidget* HeroUserWidget = Cast<UHeroUserWidget>(HealthBar->GetUserWidgetObject());
  if(HeroUserWidget)
  {
    // Sending data to health bar above character, to be seen by other players
    HeroUserWidget->SetWidgetController(this);
    const UHeroAttributeSet* HeroAttributesSet = Cast<UHeroAttributeSet>(AttributeSet);
    if(HeroAttributesSet)
    {
      AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(HeroAttributesSet->GetHealthAttribute()).AddLambda(
      [this](const FOnAttributeChangeData& Data)
      {
        OnHealthChangedDelegate.Broadcast(Data.NewValue);
      });
      
      AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(HeroAttributesSet->GetMaxHealthAttribute()).AddLambda(
      [this](const FOnAttributeChangeData& Data)
      {
        OnMaxHealthChangedDelegate.Broadcast(Data.NewValue);
      });
      
      OnHealthChangedDelegate.Broadcast(HeroAttributesSet->GetHealth());
      OnMaxHealthChangedDelegate.Broadcast(HeroAttributesSet->GetMaxHealth());    
     }
  }``` 
Same code as before, but now I can use this function and call it on the Character too, and calls it on two places,
```PossesedBy``` and ```OnRep_PlayerState```
In the WBP I Cast to CharacterBase and works for everyone now in multiplayer.
#

[SOLVED] health bar replication change for all Characters

#

[SOLVED] Health bar replication change for all Characters

kindred crane
#

Hi @hardy ledge, I know you already solved this but maybe you want to check my post.