LoveLim
(LoveLim)
December 14, 2017, 1:51am
1
Hi
I have a problem to put widget in my Character Class.
my GameMode class has a UUseWidget already like below.
.h
public:
UFUNCTION(BlueprintCallable, Category = "UMG")
void ChangeMenuWidget(TSubclassOf<UUserWidget> NewWidgetClass);
UUserWidget* GetCurrentWidget() const;
protected:
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "UMG")
TSubclassOf<UUserWidget> StartingWidgetClass;
UPROPERTY()
UUserWidget* CurrentWidget;
};
.cpp
void ADDModeBase::BeginPlay()
{
Super::BeginPlay();
ChangeMenuWidget(StartingWidgetClass);
}
void ADDModeBase::ChangeMenuWidget(TSubclassOf NewWidgetClass)
{
if (CurrentWidget != nullptr)
{
CurrentWidget->RemoveFromViewport();
CurrentWidget = nullptr;
}
if (NewWidgetClass != nullptr)
{
CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), NewWidgetClass);
if (CurrentWidget != nullptr)
{
CurrentWidget->AddToViewport();
}
}
}
UUserWidget* ADDModeBase::GetCurrentWidget() const
{
return Cast<UUserWidget>(StartingWidgetClass);
}
Here I want to use StartingWidgetClass that is a UUMG_PlayUI class in myCharacter Class.
I had to create UUserWidget* Name; variable. but i can get that StartingWidgetClass.
Actualy, UUMG_PlayUI! without CreateWidget<>.
How to use a userwidget’s function in my character class?
Do I understand you right, that you want to call a function which you declared in your widget blueprint?
I don’t think that is directly possible. The way, you can call blueprint functions is by declaring a BlueprintNativeEvent in your C++ class and have a blueprint implementing it. Then you can call it in your code and the logic inside the blueprint gets executed.
LoveLim
(LoveLim)
December 14, 2017, 2:38am
3
I have created some function in UUMG_PlayUI with C++. not blueprint.
void ACharacterPC::BeginPlay()
{
Super::BeginPlay();
ADeepForestGameModeBase* GameMode = (ADeepForestGameModeBase*)GetWorld()->GetAuthGameMode();
UMG = CreateWidget<UUMG_PlayUI>(GetWorld(), GameMode->StartingWidgetClass);
}
This is the code to referencing the widget.
The UMG can’t get the correct widget. This is the problem.
Some way, I can get the UUMG_PlayUI. but created variables are null.
So the problem is that UMG variable in Character is null? What is the type of this variable and is StartingWidgetClass set?
There is a problem in your code: Your GetCurrentWidget function will likely return a nullpointer
LoveLim
(LoveLim)
December 14, 2017, 3:08am
5
Right. the GetCurrentWidget is not using currently.
I might don’t know how to get UserWidget from the world().
UUMG_PlayUI::UUMG_PlayUI(const FObjectInitializer& PCIP) :Super(PCIP)
{
textDisplay = nullptr;
textComboBox = nullptr;
}
void UUMG_PlayUI::NativeConstruct()
{
Super::NativeConstruct();
if (textDisplay == nullptr)
{
textDisplay = (UTextBlock*)GetWidgetFromName(TEXT("DisplayText"));
textDisplay->SetText(FText::FromName(TEXT(" ")));
}
if (textComboBox == nullptr)
{
textComboBox = (UTextBlock*)GetWidgetFromName(TEXT("ComboBox"));
textComboBox->SetText(FText::FromName(TEXT(" ")));
}
}
void UUMG_PlayUI::SetCombo(int32 ComboCount)
{
FString Info = FString::Printf(TEXT(“COMBO : %d”), ComboCount);
textComboBox->SetText(FText::FromString(Info));
}
void UUMG_PlayUI::SetDisplay(FString Info)
{
textDisplay->SetText(FText::FromString(Info));
}
This is my UMG_PlayUI class. In Character class, want to call the SetCombo function like
SetCombo(ComboCount);