The game I’m working on has collectables (“CollectableBP”). PlayerHUD is loading “CollectablesIndicator” widget which should update text if the certain criteria upon pickup is met.
Example: “You don’t have enough items X”, “You need one more item X”, “You have all the items you need”.
I’m currently using EVENT TICK (instead of event construct) inside my widget’s blueprint, because it constantly triggers the widget’s logic. It is working great but I’m aware that this is not the right approach. I would like to make my CollectableBP communicate with the widget upon the actual item pickup, not every second.
What is the best approach to refresh / rebuild widget’s event construct through my CollectableBP?
Most of the time the answer on this question is: Event Dispatcher. To be honest, I’ve never used it until now, but as far as I can tell, it’s like the Widget is listening for events - triggered by the CollectableBP.
While I was researching on how to solve my problem, I’ve stumbled upon event dispatchers but I was unable to find a “normal” and beginner friendly tutorial on YouTube. I’ll definitely check this link out.
Anything else we need to know about? As in, the pickup actors are spawned dynamically as we progress through the game? Or there’s a limited amount placed in the level manually?
Atm, the widget is pulling the data from the player on Tick, correct?
Collectables are dynamically spawned across the level (“CollectableBP”)
Upon a pickup → pickup values are stored (as booleans / integers) inside ThirdPersonCharacter blueprint → actor is destroyed (inside “CollectableBP”)
“CollectablesIndicator” widget blueprint handles the logic and updates the display
My goal is to connect steps 3 and 4.
CURRENTLY - the widget is using EVENT TICK which constantly checks for booleans / integers values and updates the display via text transform.
IDEALLY - “CollectableBP” would “ping” the widget after destroying the actor so that the widget could recalculate and refresh the display via text transform.
But which blueprints creates this widget? The player? While you absolutely can use a dispatcher for this, there may be no need.
Player runs into the pickup and updates its own hud widget. Basic direct comms would be more than enough here.
If the widget is created by the Game Mode for example, and thus is unrelated to the player blueprint, a dispatcher will be a better solution.
What you call Ideally may not be that perfect. Generally speaking, it’s the player that should handle the counting and the widget should be displaying what the player has counted. The fewer elements there are in the widget, the better.
In this case, the player creates the widget and shows it. When the player runs into a collectible, we count it and set the widget’s text to that value. Here the widget was updated 4 times.
So what is Player_HUD - another widget? The HUD class? Just trying to wrap my mind around it. The player HUD widget creates another widget - the Collectible widget?
I have tried creating the widget and adding it to the viewport from the CollectableBP after actor destroy event and it worked but it loaded one widget over another instead of updating it.
If you’d like to see an example taking advantage of an Event Dispatcher handling it, do tell. What spawns the collectibles?
If you wish the player knew nothing about the collectibles and prefer they talked directly to the HUD’s widget when they’re picked up, it can be arranged, too. Not a bad approach for a game where there are multiple characters.
Wow! This example is amazing! I’ll definitely try the Cast to Player_HUD (with widget/text-block) approach! Thanks!
As for event dispatcher, it would be amazing to see how that works as well. I’d like to learn about Unreal as much as I can, so I always find interest in different approaches.
Collectables are spawned through SpawnerBP - it is manually placed on multiple locations across the level and it has a chance to spawn multiple collectable types or to spawn nothing.
The idea behind Dispatchers is that one actor calls it and another actor receives that call. It makes it somewhat easier if you think about it as broadcasting and listening. Actors broadcast (call dispatchers) and other actors can listen to those calls. Actors that don’t listen cannot react to those calls.
In this instance the HUD class will listen to the Collectible actors’ calls.
Interestingly enough, actors already come with a bunch of dispatchers. You can add your own custom ones, but it’s not necessary here. Since the collectibles are getting Destroyed, we’ll use that.
IDEALLY - “CollectableBP” would “ping” the widget after destroying the actor so that the widget could recalculate and refresh the display via text transform.
(bottom) A Custom Event with an Actor input - the input is required here as matching signatures on Dispatchers is a thing. When this event is called, it will update the text in the widget. Think of this element as the listening bit - it will listen to the collectible actors getting Destroyed.
The collectible actor:
(top) at Begin Play, aCollectibleBP registers its own (self) dispatcher call (Bind Event to On Destroyed) with the Custom Event in the HUD (pic 1).
Actors already broadcast their own destruction, but hardly anyone listens :(. Now the HUD will be notified when it happens and that