Hi, please help me.
I’ve already posted another question on StackOverflow and here in the Unreal Engine Forum about a specific problem that I currently have in my project, but nobody seems to reply.
So I’ll resume this shortly: I wanna make an HealthComponent (a C++ class) for my character, and also display the HP with an HealthBar Widget Blueprint.
Now I’m not a big fan of Copuling classes together, so I always use Events in these cases. The thing is that the delegate I’m trying to fire up (in my case OnHealthSet) seems not to work. Whenever I call the:
OnSetHealth.Broadcast(MaxHealth, CurrentHealth);
nothing happens. I don’t get if I’ve missed some steps in order to make this work. I’m used to Unity events since I’ve been using it for 3 years, but in Unreal Engine this seems too convoluted.
This is the HealthComponent.h file:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HealthComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class RPG_TEST_API UHealthComponent : public UActorComponent
{
GENERATED_BODY()
public:
UHealthComponent();
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(
FOnSetHealth,
float, MaxValue,
float, CurrentValue
);
UPROPERTY(BlueprintAssignable) FOnSetHealth OnSetHealth;
UFUNCTION(BlueprintCallable) int GetHealth() { return CurrentHealth; }
UFUNCTION() void SetHealth(const int Value);
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere) unsigned int MaxHealth;
UPROPERTY(EditAnywhere) unsigned int CurrentHealth;
};
While this is the HealthComponent.cpp file:
#include "HealthComponent.h"
// Sets default values for this component's properties
UHealthComponent::UHealthComponent()
{
}
void UHealthComponent::BeginPlay()
{
Super::BeginPlay();
SetHealth(MaxHealth);
}
void UHealthComponent::SetHealth(const int Value)
{
CurrentHealth = (Value < 0) ? 0 : Value;
CurrentHealth = (Value > (int)MaxHealth) ? MaxHealth : Value;
OnSetHealth.Broadcast(MaxHealth, CurrentHealth);
}
And this is the HealthBar Blueprint which I’m trying to subscribe to the event: