I don't get how Delegates / Events works on Unreal Engine

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:

If it an event dispatcher, it will need binding.

If it just a custom event(the one in red represent create), you will to call it (in blue).

So it will depend how you bind the event or how you are calling the custom event “OnSetHealth”

Like @Arodi007 said, you have to bind an event to the delegate
In blueprint this is done by dragging from an object reference and using the Bind/Assign to Event options

For example in your case, you can drag from the health component after setting it, and search for Assign Event to OnSetHealth, which will automatically create a bind node, and a new event with the same signature that will be bound to the delegate


I think this should be your solution. The fact is that I’ve already done that but still this doesn’t work. I think the real problem is the

OnSetHealth.Broadcast(MaxHealth, CurrentHealth);

instruction in the cpp file, because I’ve also tried to subscribe a simple function to the delegate and it didn’t do anything

no that is correct
Unless you’re calling set health in other places, begin play has most likely already run by the time you bind the event in the widget, which would explain why you don’t get the callback

You can place a breakpoint on the broadcast line and see through the debugger what is bound to your delegate. If you don’t see any function in the invocation list that means that nothing been bound.

Well I also putted the broadcast instruction in the Tick function to check what would happen, but nothing