Adding UI Over Actors Without Pre-Assigning A Blueprint

I’m trying to add a widget or some other form of UI over some actors that I have spawned. The problem I’m running into, is that I have a class that spawns actors, it builds them in code, there is no reference to a template. These actors need to show some text data over themselves, basically I’m working on an A* algorithm and I want the tile values to show for debugging. All of the guides I’m finding online are suggesting that I build widgets and put them into the world and pull data for them at Begin play, but since my actors aren’t already in the world, I don’t have anything to bind them to. I may be able to refence the widgets file location and spawn that way, but that seems like really bad practice. Are there any suggestions on how I could go about this? I’m newer to the Unreal API and its impossible to find good documentation on just about anything.

If you are spawning tile actors from code, you can also spawn widget actors on top of them from code.

Alternatively you can add a (single) widget to the player viewport, perform line traces to where the player is looking, and draw debug information when the trace hits one of your tile actors.

1 Like

All of the examples I see out there though are creating widget classes that blueprints will inherit from. Can I just build a widget in code and spawn it, which class would I use for that, UWidgetComponent? Thanks for your input

  1. Create widget in editor (user interface → widget blueprint)
  2. Create a new BP actor with a WidgetComponent to display your widget
  3. Spawn those BP actors from c++ code

To spawn BP actors from c++ code, if it’s just for debugging you don’t really need to worry about good practice and can hardcode the path to the blueprint class to spawn. Example:

UClass* Class = LoadClass<AActor>(nullptr, "/Game/Blueprints/BP_DebugWidgetActor.BP_DebugWidgetActor_C`) 

If it’s more than that, you should define a class variable in a BP-overriden singleton such as GameMode. Add the property in your native GameMode class :

UPROPERTY(EditAnywhere)
TSubclassOf<AActor> WidgetDebugActorClass;

Then create a child BP class of your gamemode and point the reference in its defaultproperties.
Then make sure the level/project is using your custom gamemode class.
Then access it from cpp code via GetWorld()->GetAuthGameMode<AYourGameMode>()->WidgetDebugActorClass.

1 Like

I’ll try that out tonight, thanks!
Would I be able to communicate with the BP through C++ though? I need to widgets to display tile costs, thise values will change as I select different destinations, so I need to be able to talk to the widgets and tell them their values are different.

The simplest way to achieve this type of direct communication between an actor and it’s spawned UWidgetComponent is to create a custom C++ UUserWidget based class. While creating the widget blueprint in the editor, click Class Settings and reparent the editor widget to the custom C++ UUserWidget class. Once this is done, you can use UFUNCTION(BlueprintCallable) decalred methods to access any data you define in the C++ UUserWidget class.

UCLASS()
class USomeCustomWidget : public UUSerWidget
{
private:
int32 Data;

public:

UFUNCTION(BlueprintCallable)
int32 GetSomeData() { return Data; }

}

While creating your widget blueprint in the editor, click on the Class Settings on the toolbar, change the widget blueprint parent to , in this example, USomeCustomWidget. Save and compile the widget blueprint. Then in the event graph you can access the method GetSomeData() through blueprints to retrieve whatever data.