How to set widget using ConstructorHelpers?

TargetDummy.h:

TSubclassOf<UUserWidget> WidgetBPContainer;

TargetDummy.cpp:

ATargetDummy::ATargetDummy()
{
	LockedOnWidget = CreateDefaultSubobject<UWidgetComponent>("WidgetComponent");
	LockedOnWidget->SetupAttachment(RootComponent);
	LockedOnWidget->SetWidgetSpace(EWidgetSpace::Screen);

	static ConstructorHelpers::FClassFinder<UUserWidget> WidgetContainer(TEXT("WidgetBlueprint'/Game/Widgets/LockOnIndicator.LockOnIndicator_C'"));
	if (WidgetContainer.Succeeded())
	{
		WidgetBPContainer = WidgetContainer.Class;
	}
}

Screenshot (65)

Just tested with UClass* instead of TSubclassOf and still doesn’t work.

Let me get your logic right. So you have an external widget class WidgetBPContainer that you are setting inside of the editor.

You must understand that when you use TSubclassOf you are telling unreal
“I’m setting this variable as an instance of User Widget in the editor”

It only holds the class of the passed in widget in this case.

The line

WidgetBPContainer = WidgetContainer.Class;

Doesn’t really make sense. You are setting a class to another class.

You can only use WidgetBPContainer (a UClass) to create a widget from it. It can’t be used as a widget instance on it’s own.

You create the widget component LockedOnWidget in the constructor and that is where you want to call LockedOnWidget->SetWidgetClass(WidgetContainer.Class);

if you want the full reference to the widget then you should be using

UUserWidget* WidgetBPContainer;

That would give you a full object of type User widget.

If you are still having problems then PM me the project and I can fix it for you. It still seems rather small so it shouldn’t be a problem to upload to the cloud somewhere.

Ty that clears up a lot but it still fails to succeed when I do it that way. Also WidgetBPContainer is just used to store the Widget Blueprint given by ConstructorHelpers which I use to set the Widget Class in a function.

Here is the revised code that should match up with your project. Just swap the _API

TargetDummy.cpp (1.0 KB)
TargetDummy.h (640 Bytes)

If it’s not working then it’s a matter of moving assets to folders in unreal. Try going on all directories and right click “Fix up redirectors” and look into your file explorer that the widget is physically in the right place

That’s exactly what I did but I changed TSubclassOf to UClass* based off of what you said previously. Still no luck.

this part of the code

TSubclassOf<UUserWidget> WidgetBPContainer;

has no impact on if the widget shows up or not. It’s not used anywhere besides it being set in the constructor. It doesn’t take part in the process of showing the widget. There is no need to focus on it at the moment.

You are using the direct path to set the widget.

I know just cleaning up but the main problem is that it fails to succeed since it can’t find the file for some reason.

I’ve also tested the function separately and it works like intended.

Maybe try deleting temp directories & rebuilding the project?
It should work at this point.

Sry but how do I do that exactly. I’m not sure which files to delete.

Make sure none of your header or cpp files are in intermediate by accident (vs can sometimes save there). All of your c++ code should be in you “Source” directory

Select temp folders
1
Right click uproject file and Generate.
2
Project should be rebuild
3
double click project file as normal. Build & run from IDE

Still didn’t fix it.:frowning: Dw I’ll find a way.

Ok so I’ve managed to make it work but I don’t know why. I just loaded it up, replaced a few things and it just decided to work for some reason. Here’s the solution but it’s a solution I’ve tried in the past so I don’t know why it works now.
TargetDummy.h

TSubclassOf<UUserWidget> WidgetBPContainer;

TargetDummy.cpp(Constructor):

static ConstructorHelpers::FClassFinder<UUserWidget> WidgetContainer(TEXT("WidgetBlueprint'/Game/TargetDummyWidgets/LockOnIndicatorWidgetBP.LockOnIndicatorWidgetBP_C'"));
	if (WidgetContainer.Succeeded())
	{
		WidgetBPContainer = WidgetContainer.Class;
	}

I used TSubclassOf instead of UUserWidget* as in the cpp file there’s no suitable conversion function to convert from UUserWidget* to TSubclassOf could anyone explain this to me?

Again thanks to @3dRaven for all the help and support, you helped tons and I am forever grateful, you’re amazing!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.