devilhiyan
(devilhiyan)
January 10, 2018, 9:09pm
1
hi , I made canvas panel and a button in c++ but they are not being shown in designer editor hierarchy for further editing and placement. Is there some kind of UPROPERTY() or something, or is it a completely different concept. Any help is appreciated.
Thank you.
xlar8or
(xlar8or)
January 10, 2018, 9:32pm
2
Hey there, if they derive from UUserWidget then it should show up, try to restart the engine.
devilhiyan
(devilhiyan)
January 11, 2018, 3:54pm
3
thanks for the reply, please take a look again I added an image to clarify things
xlar8or
(xlar8or)
January 11, 2018, 4:16pm
4
What type of changes did you do on the button?
devilhiyan
(devilhiyan)
January 11, 2018, 6:20pm
5
here is code for Widget
.h file -
#include “CoreMinimal.h”
#include “Blueprint/UserWidget.h”
#include “UMG.h”
#include “RestartWidget.generated.h”
UCLASS()
class TEST_API URestartWidget : public UUserWidget
{
GENERATED_BODY()
protected:
virtual void NativeConstruct() override;
virtual TSharedRef RebuildWidget();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UButton * RestartButton;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UPanelWidget *RootWidget;
UFUNCTION()
void OnRestartClicked();
};
.cpp file -
#include “RestartWidget.h”
#include “Runtime/UMG/Public/UMG.h”
void URestartWidget::NativeConstruct()
{
Super::NativeConstruct();
}
TSharedRef URestartWidget::RebuildWidget()
{
if (WidgetTree)
{
RootWidget = Cast(GetRootWidget());
if (RootWidget == nullptr)
{
RootWidget = WidgetTree->ConstructWidget(UCanvasPanel::StaticClass(), FName(“RootWidget”));
UCanvasPanelSlot * RootSlot = Cast(RootWidget->Slot);
if (RootSlot)
{
RootSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
RootSlot->SetOffsets(FMargin(0.f, 0.f));
}
WidgetTree->RootWidget = RootWidget;
}
}
TSharedRef<SWidget> Widget = Super::RebuildWidget();
RestartButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("RestartButton"));
RootWidget->AddChild(RestartButton);
UCanvasPanelSlot* RestartButtonSlot = Cast<UCanvasPanelSlot>(RestartButton->Slot);
RestartButtonSlot->SetAnchors(FAnchors(.5f, .5f, .5f, .5f));
RestartButtonSlot->SetSize(FVector2D(300.f, 180.f));
RestartButtonSlot->SetAlignment(FVector2D(.5f, .5f));
RestartButton->OnClicked.AddDynamic(this, &URestartWidget::OnRestartClicked);
return Widget;
}
void URestartWidget::OnRestartClicked()
{
UGameplayStatics::OpenLevel(this, FName(*()->GetName()), true);
}
in editor -
xlar8or
(xlar8or)
January 11, 2018, 6:24pm
6
Please surround every part of the code with the code tag or else its really hard to read.
devilhiyan
(devilhiyan)
January 11, 2018, 6:27pm
7
not sure what happened i will paste the code again
.h file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "RestartWidget.generated.h"
/**
*
*/
//class UButton;
UCLASS()
class TEST_API URestartWidget : public UUserWidget
{
GENERATED_BODY()
protected:
virtual void NativeConstruct() override;
virtual TSharedRef<SWidget> RebuildWidget();
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UButton * RestartButton;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
class UPanelWidget *RootWidget;
UFUNCTION()
void OnRestartClicked();
};
devilhiyan
(devilhiyan)
January 11, 2018, 6:28pm
8
c++ file
// Fill out your copyright notice in the Description page of Project Settings.
#include "RestartWidget.h"
#include "Runtime/UMG/Public/UMG.h"
void URestartWidget::NativeConstruct()
{
Super::NativeConstruct();
}
TSharedRef<SWidget> URestartWidget::RebuildWidget()
{
if (WidgetTree)
{
RootWidget = Cast<UPanelWidget>(GetRootWidget());
if (RootWidget == nullptr)
{
RootWidget = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), FName("RootWidget"));
UCanvasPanelSlot * RootSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);
if (RootSlot)
{
RootSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
RootSlot->SetOffsets(FMargin(0.f, 0.f));
}
WidgetTree->RootWidget = RootWidget;
}
}
TSharedRef<SWidget> Widget = Super::RebuildWidget();
RestartButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("RestartButton"));
RootWidget->AddChild(RestartButton);
UCanvasPanelSlot* RestartButtonSlot = Cast<UCanvasPanelSlot>(RestartButton->Slot);
RestartButtonSlot->SetAnchors(FAnchors(.5f, .5f, .5f, .5f));
RestartButtonSlot->SetSize(FVector2D(300.f, 180.f));
RestartButtonSlot->SetAlignment(FVector2D(.5f, .5f));
RestartButton->OnClicked.AddDynamic(this, &URestartWidget::OnRestartClicked);
return Widget;
}
void URestartWidget::OnRestartClicked()
{
UGameplayStatics::OpenLevel(this, FName(*()->GetName()), true);
}
devilhiyan
(devilhiyan)
January 11, 2018, 6:32pm
9
please take a look at the code below
MichaelWion
(MichaelWion)
February 12, 2018, 10:55pm
10
Was a solution ever found for this?
I think you need to add BindWidget meta specifier to declare widget as part of the userwidget, since UE4 by default don’t really know for what you gonna use this pointer for, you could
UPROPERTY(EditAnywhere, BlueprintReadWrite,meta=(BindWidget))
class UButton * RestartButton;
this is not working, it’s giving following error
non-optional widget binding Restart Widget not found.
non-optional widget binding Root Widget not found
Atheist91
(Atheist91)
March 9, 2018, 10:31am
13
Afaik this meta works for widgets you manually place in UMG designer. Additionally you have to somehow specify widget name (possibly it’s c++ variable name) and set exact same name on the widget in designer. Then it should automagically put that widget in your property.
I didn’t test it tho.