Accessing non-character Actor in widget blueprintnfor event driven UI

So I am new to Unreal Engine 4 and was reading this in the documentation:

https://docs.unrealengine.com/en-US/...dUI/index.html

And basically I like the event driven UI system. My only problem is that in the tutorial (and pretty much every example online) only covers how to do this with the Get Player Character node. What if I want to receive events from a non-global, non-character object in my game? How would I access that in the widget blueprint? So say I have a BP which is derived from a C++ class and I have declared a delegate which I want my widget blueprint to bind to. The problem is, I have no way to access this BP in my widget blueprint. How could I solve this? Thanks!

Do it through your player character usually every widget you use is somehow connected to your character in one way or another, first I suggest to you to have the main widget of hud(all of your widgets should go in it) in your Player Controller, through it you can get the controlled player be using Get Controlled Pawn and doing a cast of your Player Character. From there you create the hud save it as a variable and give him your Player Character. Now when you need to lets say make a widget appear you through your player call for your PlayerController->Hud->Widget-> Set to Hidden false, something like that and if you want to do changes to the player through some widget you already have the player reference in the Hud and that how you create a simple two way hierarchy. Good luck.

how does this object gets into the world? If you spawn that Object (I guess it is aActor?), that’s the moment you should connect those two or at least save a reference in that actor from where you spawn. Like if your pawn spawns some managerBP, after spawning it bind your hud to that delegate. when your actor is already in the world without spawning it manually, things get a little bit more tricky. can you go a little bit into details what you plan to do?

Don’t you think it’s unreasonable to always put stuff in a character? Like you could have 200 objects in your game that wants to control UI that have nothing to do with the character…I don’t believe this is a rare thing in games. Your character blueprint/class would get extremely bloated and you’d have stuff in there that has nothing to do with the player. Like in this case my BP is just a zone and I just want to be able to access this zone in my widget I don’t think this should be hard.

I drag it from the content browser into the scene. Do you know if there’s a way in the widget blueprint to like check if some specific actor in the scene has been constructed (without GetAllActorsOfClass)? Because then I could bind to that in the widget blueprint and save it as a reference.

Are you talking about a pawn as let’s say widget of health for a fence, then you do the same thing with a simple hierarchy be giving the pawn reference to the widget, use expose at spawn for the variable or just set it manually if you add the widget the actor viewport.

I guess I still don’t understand…but I try :slight_smile: if you want lets say add actors to the level while levelbuild and reference them in a BP (or widget-BP), I would add a variable in the BP (some dedicated actor for managing stuff) of type actor (maybe array of actor) and ObjectReference (check instance Editable), then you can add that managerBP to the level, and pick from the level what you want to reference, and in your managerBP get that variable (maybe a validatedGet or a isValid Node) and then you can directly do your binding. I’m not a big fan of the levelBP - but this could also be a good option in your case when the actors are all created during level build. If not - add a custom Event to your actors which bind to the UI, after your Widget is added to the viewport getAllActorsOfClass(yourActor)->forEach->callCustomBindEvent … not pretty - but would also do the trick.

can you say a little bit more what you plan to do? It sounds strange that “200 objects in your game that wants to control UI” - normally they effect a pawn or character and that one controlls the UI.

Ok so your solution with getAllActorsOfClass might work but I don’t like using that method it’s not pretty like you said. When I said the thing about 200 objects it was just an example scenario. Basically, what I am saying is like you could have various objects in your game (cubes, walls, trees, whatever) that are not pawns. These objects might have variables that you want to display in a UI. For example, a health bar above a wall or something. My question is like how would you bind the UI to these objects? How do you get those references? These walls and objects are not global like getPlayerController so how do you get access to them without getAllActorsOfClass?

Can u explain that again? A fence is not a pawn…

A fence is an example of a pawn… again as I understood you just drag the widget into the world with you should never do, if you enter to your pawn actor you can add that widget as every other component just add a widget component and change the class to your widget and then just from the BeginPlay set the variable reference of the widget to this same pawn…

No I never dragged the widget blueprint into editor… I dragged out the other actor which I want to reference in the widget blueprint.

You obtain references by interacting with the world, chiefly line tracing or collision. Imagine a world where everything is generated procedurally - you can’t possibly hook it all up upfront.

You can hook it all up dynamically when the interaction happens. Player looks at an object and presses the button, the line trace returns hit object reference - you can now spawn a widget and use its delegates to pass data to-and-fro.

Hit us up with a real life example.

I already wrote how to do the reference just do that then…

I understand what you are saying. But isn’t it limiting to only be able to get references after interactions? How would the game even get started in the first place? For you to have interactions, there already needs to be stuff happening at the start. So in my example, I have a blueprint (say like a cube object) and I drag out this blueprint into the scene from the content browser. How do I get a reference to this cube blueprint in a widget blueprint?

So in my example, I have a blueprint
(say like a cube object) and I drag
out this blueprint into the scene from
the content browser. How do I get a
reference to this cube blueprint in a
widget blueprint?

This phrase by BlueMind Studio sums it up:

how does this object gets into the
world?

Yeah, it will depend on where that widget is created:

  • was it created by another object placed the Level Blueprint
  • was it created by the Level Blueprint itself
  • was it created by any of the framework classes (Pawn, Game Mode, Player Controller…)
  • was it created by another, dynamically spawned object

Do tell what your scenario is and I’ll drop a screenshot on how I’d approach that.


People have been mentioning AllWidgets/ObjectsOfClass - as much as I hate that node as it leads to bad practises (when misused) it does have its niche and can be useful. My biggest gripe with it that it does not guarantee a particular order of elements so it’s not too reliable.

Also, it’s absolutely possible to have an orderly communication between object-widget-object without creating hard references. The system is somewhat obscured but reasonably fleshed out. Dispatchers and delegates make it possible to hook everything up dynamically.

can you say a little bit more what you
plan to do? It sounds strange that
“200 objects in your game that wants
to control UI” - normally they effect
a pawn or character and that one
controlls the UI.

This is actually a pretty common scenario for procedurally generated content. You create x objects, bind their onDestroyed event to the UI and forget about it. No more wires or references. Whenever an object gets destroyed, the UI gets the message.

So the widget is created in a custom HUD blueprint and the other blueprint(the one I want a reference to in the widget) is created by dragging it out from the content browser into the scene.

So the widget is created in a custom
HUD blueprint

Are you talking about the [HUD class][1]? To clarify, this is HUD class:

292401-annotation-2019-11-15-152559.png

Or another widget creates this widget?

Or your custom HUD blueprint is not a widget, just an actor?

Yea so it’s through a custom blueprint that’s derived from HUD and then I set that as the HUD class in game mode. That’s how the widget gets created.

And here I was thinking I’m the only one still using the HUD class.

So the HUD creates a widget. It’s a framework class accessible from anywhere anytime, so you can hook up anything to it anytime.

Ensure the HUD does store a reference to the widget and then the world actors can access the the HUD like so:

In the above example the Black Holes are the actors dragged into the Level Blueprint (from the content browser to the scene). The Black Hole actor has an Event Dispatcher; when called, it will broadcast a float (it can be seen in the event signature). The level blueprint iterates through its (3 black hole) objects and registers an event in the HUD. From now on, whenever a Blackhole calls its event dispatcher, the widget in the HUD will know about it.


If you do not want use the Level Blueprint as glue between objects, each actor can register like so instead:

The end result is the same, though. The only hard reference needed here is between the HUD and its widget: