Are there any events that are called AFTER Begin Play

Hi.
I’ve already posted two posts regarding a specific problem that I’ve been facing for days without any solution:

But now after days of debugging the whole code I think I’ve found where the problem is.

So basically I have an HealthComponent (C++) that fires a Delegate everytime the Health is set: OnSetHealth. The listener of this delegate is in my WBP_HUD, that is a Widget Blueprint, because I want to update the Health Bar every time the event is fired.

Ideally I would broadcast the delegate in Begin Play like this:

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

	SetHealth(MaxHealth);
}
void UHealthComponent::SetHealth(const int Value)
{
	CurrentHealth = FMath::Clamp(Value, 0, MaxHealth);
	OnSetHealth.Broadcast(MaxHealth, CurrentHealth);
}

while this is the logic inside the WBP_HUD:

The problem is that Begin Play is always called BEFORE any WBP event like On Initialized or Event Constructor, because they get called AFTER the UI has been displayed, which means it doesn’t bind the event in time.

One work around that I’ve found was broadcasting the delegate in the Tick event just for the first frame, but this obviously seems pretty Spagetti code:

void UHealthComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	if (bFirstTick)
	{
		SetHealth(MaxHealth);
		bFirstTick = false;
	}
}

So for this reason I’m asking if there’s any event that are called after Begin Play

i mean you could create you own Event but i dont think thats the point.

I think your problem here is load order and you’ll encounter this alot, what you should do is just control it somewhere, ie the PlayerController spawns the pawn and the creates the widget so you know the pawn exists when the widget is created

So you think that I should create my own PlayerController and load the UI and the Pawn at the same time? Wouldn’t Begin Play still get called before the UI is loaded? This seems very odd as a work around but I’ll try it

i’d say this is standard practice,
the problem as you’ve identified is you just don’t know when BeginPlay will fire, worse it can change between PIE and Standalone breaking your project and can be hard to track down.

consider my way like a load screen, you load all the things you need in the order they require and so it will always work.

another advantage to this is you can pass the newly created pawn/component straight to the UI and not have to Get/Cast IF that’s the design you want

Well I’m not familiar with custom Player Controllers. Can you show an example of how to do it?

you can create one the same as any class in the NewBlueprintClass menu or in c++ just inherit from APlayerController

then in your world settings/gamemode change the default controller class to your class and default pawn to none as you will spawn your own.

on controller begin play spawn the pawn and possess