I have a class Act_31
and from it I want to call function, which change the text inside widget.
There is a virtual function NativeConstruct()
, which calls automatically. And it change text of widget, but I need to call function F1
and send text to it, which I want to display. But when I call it, variable Name
is null.
And by some reason it is not null, when program call NativeConstruct()
.
From here I call function from another class
void AAct_31::BeginPlay()
{
Super::BeginPlay();
UAccessWidgetFromCpp* accessWidgetFromCpp = NewObject<UAccessWidgetFromCpp>();
accessWidgetFromCpp->F1("222");
widgetDialogBottomInstance->AddToViewport();
}
.h
UCLASS()
class HOME_API UAccessWidgetFromCpp : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
class UTextBlock* Name;
UFUNCTION()
virtual void NativeConstruct() override;
UFUNCTION()
void F1(FString text);
};
.cpp
#include "AccessWidgetFromCpp.h"
void UAccessWidgetFromCpp::NativeConstruct()
{
if (Name) // Name not null
{
Name = (UTextBlock*)GetWidgetFromName(TEXT("Name"));
Name->SetText(FText::FromString("111"));
}
}
void UAccessWidgetFromCpp::F1(FString text)
{
if (Name) //Error: Name is null
{
Name = (UTextBlock*)GetWidgetFromName(TEXT("Name"));
Name->SetText(FText::FromString(text));
}
}