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?
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:
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
Aah okay, thank you 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 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;
}