Hi everyone,
I am working on a project that uses widget blueprints for UI element layout and C++ for all logic. A simple example is a widget blueprint that consists of a checkbox (UCheckBox) and another widget blueprint.
I created a parent class that has two variables that hold the two UI elements (checkbox and other blueprint) and a method, and I want to manipulate the UI elements from the method. I also created a parent class for the other blueprint the same way.
Here’s how my class looks:
QuestCheckbox.h
UCLASS()
class UNREALQUESTBP56_API UQuestCheckbox : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (BindWidget), Category="UI Setup")
UQuestBasicText* BasicTextWidget_Answer;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (BindWidget), Category="UI Setup")
UCheckBox* CheckBox_Answer;
UFUNCTION(BlueprintCallable)
void SetText(FString Answer) const;
};
QuestCheckbox.cpp
void UQuestCheckbox::SetText(const FString Answer) const
{
BasicTextWidget_Answer->DisplayedText = FText::FromString(Answer);
}
QuestBasicText.h
UCLASS()
class UNREALQUESTBP56_API UQuestBasicText : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(BlueprintReadWrite)
FText DisplayedText;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (BindWidget))
UTextBlock* TextBlock;
};
And here’s how I use those:
UQuestCheckbox* checkBox = CreateWidget<UQuestCheckbox>(this, UQuestCheckbox::StaticClass());
checkBox->SetText("LULZ");
The second call crashes because BasicTextWidget_Answer is null. So I assumed elements in widget blueprints are not constructed immediately when the widget is instanced. I added checkBox->Construct(); before calling SetText() but that did not help. What am I missing here?