Programming default widget in C++

LuisAlaguna
Mon, 17 Jun 2019 14:01:20 +0000
UI

I’m trying to create a new HUD for most of common levels in my game; it must to see like this:

As you can see i can handle it as a BP widget, it is no so hard, but i want to do it in c++. So, first i created a new HUD class by mistake that has the next code:

// Create buttons and set them in a slot
    UButton* HomeButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("Home_Button"));
    HomeButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(HomeIconTexture));
    HomeButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
    HomeButton->WidgetStyle.Normal.Margin = 0.f;
    // HomeButton->SetColorAndOpacity(FLinearColor(1.f, 1.f, 1.f, 0));

    UCanvasPanelSlot* HomeButtonSlot = Cast<UCanvasPanelSlot>(HomeButton->Slot);
    HomeButtonSlot->SetAnchors(FAnchors(.0f, .0f, .0f, .0f));
    HomeButtonSlot->SetSize(FVector2D(80.f, 80.f));
    HomeButtonSlot->SetPosition(FVector2D(80.f, 80.f));
    //HomeButtonSlot->SetAlignment(FVector2D(.5f, .5f));

    UButton* MenuButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("Menu_Button"));
    MenuButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(MenuIconTexture));
    MenuButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
    MenuButton->WidgetStyle.Normal.Margin = 0.f;

    UCanvasPanelSlot* MenuButtonSlot = Cast<UCanvasPanelSlot>(MenuButton->Slot);
    MenuButtonSlot->SetAnchors(FAnchors(0, 0, 1.f, 1.f));
    MenuButtonSlot->SetSize(FVector2D(80.f, 80.f));
    MenuButtonSlot->SetPosition(FVector2D(80.f, 80.f));

    UButton* ArrowLeftButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("ArrowLeft_Button"));
    ArrowLeftButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(ArrowLeftIconTexture));
    ArrowLeftButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
    ArrowLeftButton->WidgetStyle.Normal.Margin = 0.f;

    UCanvasPanelSlot* ArrowLeftButtonSlot = Cast<UCanvasPanelSlot>(ArrowLeftButton->Slot);
    ArrowLeftButtonSlot->SetAnchors(FAnchors(1.f, 1.f, 0, 0));
    ArrowLeftButtonSlot->SetSize(FVector2D(80.f, 80.f));
    ArrowLeftButtonSlot->SetPosition(FVector2D(80.f, 80.f));

    UButton* ArrowRightButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("ArrowRight_Button"));
    ArrowRightButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(ArrowRightIconTexture));
    ArrowRightButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
    ArrowRightButton->WidgetStyle.Normal.Margin = 0.f;

    UCanvasPanelSlot* ArrowRightButtonSlot = Cast<UCanvasPanelSlot>(ArrowRightButton->Slot);
    ArrowRightButtonSlot->SetAnchors(FAnchors(1.f, 1.f, 1.f, 1.f));
    ArrowRightButtonSlot->SetSize(FVector2D(80.f, 80.f));
    ArrowRightButtonSlot->SetPosition(FVector2D(80.f, 80.f));

    Canvas->DrawItem();

I realized, searching here in the site, that i need to create my Widget and building it in the HUD c++ class, as we do in the level’s BP; it means i need to move that code to the Widget.

So, i created my widget class:

UCLASS()
class BOOKDEMO_API ULectureWidget : public UUserWidget
{
	GENERATED_BODY()

public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
class UButton* HomeButton;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
class UButton* MenuButton;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
class UButton* ArrowLeftButton;
UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
class UButton* ArrowRightButton;


protected:
	virtual void NativeConstruct() override;

};

Four buttons that i need. But i don’t get how must i to initialize the buttons.

In my second reference there is an idea how to do this, but i don’t get what is WidgetTree, can anyone explain me it?

My problem is that i don’t know how should i init the widget’s elements using C++, i cannot find a satisfactory answer. I know we can do this creating the widget’s BP and calling it from the HUD class, but i want to do it full c++, how can i do that?

Update 1:
This is my code from trying to do it fully c++: [code][2]. Unfortunately, [it does not work][3].

My referecens:

ben🌱ui – 21 May 17

Creating a UserWidget in C++

How to build user widgets base classes in C++.

Unreal Engine Forums – 16 Aug 16

Create widget in pure C++

I have read several links regarding creation of widgets in C++. One of them is this: Unfortunately that was in 2014. It’s 2 years from then. So I assume, UE is much better and support many more libraries. So with UI 4.12.5, is it really possible...

6 posts - 1 participant