Add panel widgets to UUserWidget

Hello,

I’ve made a new UUserWidget class and I’m trying to figure out how to use it. How do I add, for example vertical boxes, text, buttons, etc. to my user widget so I can make a blueprint version of it derived from this class, and they will display in the hierarchy?

Thanks in advance!

~Stefan

You can add widgets to a Widget Blueprint in the UE4 editor, you don’t need to add them via C++. Now, if you want to have access to some of the widgets in C++, you’ll need to add them to your class and use (meta = (BindWidget)) in your UPROPERTY, I’ll paste some code from one of my classes:

/**
 * 
 */
UCLASS()
class GAME_API UMenuWidget : public UUserWidget
{
	GENERATED_BODY()
	
public:

	UPROPERTY(meta = (BindWidget))
	class UButton* StartButton;

	UPROPERTY(meta = (BindWidget))
	class UButton* QuitButton;

	void SetUp() override;

	void Teardown() override;

protected:
	virtual bool Initialize() override;

private:
	UFUNCTION()
	void ClickStartButton();

	UFUNCTION()
	void ClickQuitButton();
};

The names you give these variables are important. Unreal will expect your widgets, that you drag and drop into your widget hierarchy, to have the same name and class/type, or the widget won’t compile and it will complain with something like:

As soon as I added the two widgets that the super class is declaring, as seen in the pic bellow, I can compile the blueprint and use the widgets in blueprints and C++.

You can move the widget anywhere in the hierarchy as long as it has the right name. In my actual game, where the UI is a bit more complex and there are multiple inheritance levels, my widget tree looks like the pic below. The bold highlighted items are inherited from C++ classes (I’ve emphasized them with orange arrows as well). The orther ones are not inherited and were added to make the menu look good :slight_smile:

I hope this helps :slight_smile:

Aah okay, thank you :slight_smile: Btw how is your function Initialize() defined in the .cpp? I get red underlines under “Inizialize” when calling super, but doesn’t give me any errors. What is this function for?

The method Initialize is used to initialize your widget :slight_smile: It is used to set up your widget with functionality after construction. In my implementation I add callback functions to my buttons:

bool UMenuWidget::Initialize()
{
	bool Success = Super::Initialize();
	if (!Success) return false;

	// main menu init
	if (!ensure(StartButton != nullptr)) return false;
	if (!ensure(QuitButton != nullptr)) return false;

	StartButton->OnClicked.AddDynamic(this, &UMenuWidget::ClickStartButton);
	QuitButton->OnClicked.AddDynamic(this, &UMenuWidget::ClickQuitButton);

	return true;
}

Okay, thanks again for amazing support! :slight_smile:

No problem, just keep at it :slight_smile:

You can also mark the answer as a solution, so other people will be more likely to benefit from it