I have widget dialogBottom
with element Text
and I need to change it’s text in c++. As I understand, if I use meta word, then I can write the name of widget and access it’s variables by creating variables with the same name in code. But by some reason variable Name
is always null
.h
UCLASS()
class HOME_API AAct_31 : public AActor
{
GENERATED_BODY()
UPROPERTY(BlueprintReadOnly, meta = (dialogBottom))
class UTextBlock* Name = nullptr;
virtual void Tick(float DeltaTime) override;
};
.cpp
void AAct_31::BeginPlay()
{
Super::BeginPlay();
if (Name)
{
char* t1 = "text1";
FText t2 = FText::FromString(ANSI_TO_TCHAR(t1));
Name->SetText(t2);
}
}
A concise, well-written post about BindWidget: http://benhumphreys.ca/unreal/ui-bindwidget/
if (Name)
{
char* t1 = “text1”;
FText t2 = FText::FromString(ANSI_TO_TCHAR(t1));
Name->SetText(t2);
}
really not sure why you are doing that for?
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
class UTextBlock* NameTextBlock;
BindWidget is your friend here. Your textblock in UMG MUST be named “NameTextBlock”.
if (NameTextBlock)
{
NameTextBlock->SetText(FText::FromString("Test Text"));
}
like i said, not sure why you was doing char* t1 and then converting back then calling SetText. Seems a bit long winded.
1 Like
Yes, I tried it, but in my code variable Name
is always null
Thank’s, it is really better to use SetText. But variable NameTextBlock
is always null by some reason. I write this code in class, which derived from AActor class. As I understand, it must derive from UUserWidget, but in this case I have error: Class is not an interface; Can only inherit from non-UObjects or UInterface derived interfaces
UCLASS()
class HOME_API AAct_31 : public AActor//, public UUserWidget
{
//...
}
Class wizard?
(Don’t forget the dependencies: UMG/Slate/SlateCore
I added dependencies:
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG", "Slate", "SlateCore" });
But can’t understand, what do you mean by class wizard. Should I inherit my class from dialogBottom (name of widget)
UCLASS()
class HOME_API AAct_31 : public AActor, public dialogBottom
{
no, Actors can NOT have widgets. Widgets must be in a UUserWidget class.
UCLASS()
class HOME_API UMyWidget : public UUserWidget
{
GENERATED_BODY()
public:
virtual void NativeConstruct() override;
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
class UTextBlock* NameTextBlock;
};
then in your CPP, you would have
void UMyWidget::NativeConstruct()
{
if (Name)
{
NameTextBlock->SetText(FText::FromString("Test Text"));
}
}
Now it works, thank you very much