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:
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:
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.