Component only works properly with delay

Hi all,

In order for my component to get the correct data and push to the widget, I need to add a delay node when I initialise it.
Here I push the current stamina upgrade value to a widget that updates on the players HUD and it should increase the size of the stamina bar. I use near enough the same logic on my health bar, also with a delay. I know the delay node is not ideal for multiple reasons, any suggestions for this?

You should be using a class based on AHud to drive ui logic. It’s there for a reason. Create your custom hud bp, set it in your game mode and then you can use get player controller → get hud → cast it to your hud bp or use an interface and call the needed init logic.

The hud will load properly with this logic.

Like so? Because it doesn’t make any difference. I did already have a widget that I call the playerHUD and that drives all my other logic like ammo counters and current weapon etc.

HealthCompTest.zip (60.1 KB)

I made a project to show you how to separate the hud from the player.

You can even go once step further with the newer viewmodels.

The HUD should not be in the player character or the player controller.

Clean and decoupled.

Hud class


You could further decouple the player widget from the hud with an interface but I made a direct reference as it is present for most of the game.

1 Like

thanks, I’ll try and fix this into what I’ve got

to understand the issue,
the problem is load order, when you call something on BeginPlay that references another object, you don’t know if that other object exists yet.

If you wont want to use a delay (which is a bad idea) you’d have to validate your dependencies or have an Init function that you call instead of BeginPlay when you know the required things are ready

Isn’t HUD kinda, sorta depreciated though… it seems to be in a bit of a limbo state so i avoid it

If it was on it’s way out then epic would remove it from gamemode as a parameter.