2

I have an Actor BP, inside this BP I have a UWidgetComponent with a Widget Class already selected in the Details, and that Widget Class has a TextBlock. Now, inside this BP I also have a C++ USceneComponent, this component is in charge of showing a random text, in the TextBlock mentioned above, each time the user presses a button.

This is in my USceneComponent's header file (.h)

class UWidgetComponent* QuestionWidget;
class UQuestionProjectionText* QuestionText;
class UTextBlock* QuestionTextBlock;

Then in the ".cpp" file, in the constructor

QuestionWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("CodeQuestionWidget"));

QuestionTextBlock = CreateDefaultSubobject<UTextBlock>(TEXT("CodeTextBlock"));

Then in the BeginPlay()

//Gets the WidgetComponent from the BP      
QuestionWidget = Cast<UWidgetComponent>(GetOwner()->GetComponentByClass(UWidgetComponent::StaticClass()));

if (QuestionWidget)
{
    QuestionWidget->InitWidget();

    //Gets an instance of the UMyUserWidget class, from the UWidgetComponent in the BP
    QuestionText = Cast<UMyUserWidget>(QuestionWidget->GetUserWidgetObject());

    this->QuestionTextBlock = QuestionText->QuestionTextBlock;

    //Sets the text to an empty String
    QuestionTextBlock->SetText(FText::FromString(""));
}
else
{
    UE_LOG(LogTemp, Error, TEXT("QuestionWidget was not found"));
    return;
}

Then in the TickComponent(), when the user presses the button, I use a something that looks like this

QuestionTextBlock->SetText(FText::FromString(QuestionStringsArray[9]));

The problem is that when I press the button, the text does not change in the Widget, but if I print the text that I'm passing, it does print the string, so I'm not passing an empty value to the "SetText()".

Another weird thing, is that the line in the BeginPlay that sets the text, that one works, I have changed it to a random string, instead of an empty one, and it does display it.

I don't know if when I do the "QuestionWidget->InitWidget();" I'm creating a new one separate from the one in the BP, or if I'm just missing something. If I eliminate the "QuestionWidget->InitWidget()" the widget gets initialized on time sometimes, and sometimes it doesn't.

I have some error handling in my code, but eliminated it here so that it didn't look too messy. But also, none of the Errors popup, everything goes on smoothly, only that the Widget doesn't show the updated Text.

Elias Marrero
  • 187
  • 2
  • 15

0 Answers0