For the last two days I’ve tried to figue out where’s the real problem and I’m surely going crazy because everything seems fine and I didn’t find any possible solution over the Internet.
Basically I’m making a custom HealthComponent for my RPG and I wanted to use events in order to update my Health Bar in the Widget Bluepring, or for something else in the future.
In my case I’ve declared a Dynamic Multicast Delegate named FOnSetHealth.
Again, everything seems correct and the compliler is not giving me any error but the delegate doesn’t fire. It’s like the instruction used to Broadcast the delegate is been skipped.
This is the HealthComponent.h file:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "HealthComponent.generated.h"
UDELEGATE()
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(
FOnSetHealth,
float, MaxValue,
float, CurrentValue
);
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class RPG_TEST_API UHealthComponent : public UActorComponent
{
GENERATED_BODY()
public:
UHealthComponent();
UPROPERTY(BlueprintAssignable) FOnSetHealth OnSetHealth;
unsigned int GetHealth() { return CurrentHealth; }
void SetHealth(const int Value);
protected:
virtual void BeginPlay() override;
private:
UPROPERTY(EditAnywhere) unsigned int MaxHealth;
UPROPERTY(EditAnywhere) unsigned int CurrentHealth;
};
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 Widget Blueprint where I’m trying to set the percentage of my Health Bar: