Good Day, I have been stuck in this quandary for a few hours now. It feels like it should be rather simple, yet I am hitting a snag somewhere, any assistance would be appreciated.
My overall goal, is to manipulate variables that reside within a Widget (Created in the Unreal Engine UI) in my c++ code.
I created a c++ class that inherits from UUserWidget
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "CountdownTimerWidget.generated.h"
/**
*
*/
UCLASS()
class THEPIT_API UCountdownTimerWidget : public UUserWidget
{
GENERATED_BODY()
public:
virtual void NativeConstruct() override;
void SetCountdownEventTimerTime(float TimeValue);
UPROPERTY(EditAnywhere, meta = (BindWidget))
class UTextBlock* CountdownEventTimer;
protected:
virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
};
here I set the follwing binding to a variable I have made in the UMG widget:
class UTextBlock* CountdownEventTimer;
I created the widget in the unreal engine UI, during creation, I selected my c++ class as the parent class of the newfound widget.
After creating the canvas in the widget, I added a text block and named the variable the same as what is found in the c++ class. CountdownEventTimer
Then, in the constructor of my primary character c++ file, I attempt to create an instance of the UCountdownTimerWidget class and add this widget to the viewport:
if (UCountdownTimerWidget* CountdownWidget = CreateWidget<UCountdownTimerWidget>(Cast<APlayerController>(Controller), UCountdownTimerWidget::StaticClass()))
{
CountdownWidget->AddToViewport();
}
This code is executed as I expect when I hit the break points during runtime (one oddity, is that the variable CountdownEventTimer is NULL at this point, though I assumed this would be set after render in the viewport).
However, when this line runs, nothing ever appears on the viewport displaying the widget.
Additionally,
I have a global event that performs an action every 30 seconds, and this widget is going to be part of the countdown timer displaying when the event is going to happen, hence the setter in the UCountdownTimerWidget class:
void SetCountdownEventTimerTime(float TimeValue);
This setter does the following:
void UCountdownTimerWidget::SetCountdownEventTimerTime(float TimeValue)
{
if (CountdownEventTimer)
{
const FString TimeString = FString::SanitizeFloat(TimeValue);
const FText TimeStringText = FText::FromString(TimeString);
CountdownEventTimer->SetText(TimeStringText);
}
}
(NOTE - The timer value I pass is a float, so I must perform this unique serialization into a string)
This line is called, CountdownEventTimer is NULL so the if condition is never satisfied, thus my value is never set.
The problem is 2 parts:
- How to render the widget to the viewport so it is displayed
- How do I change this variable since its seems to be NULL and at what point is the value set?
Additional Details:
- Here is an image of the Countdownevent_BP which is the widget (showing the parent class as the proper class I created in c++
I created this BP by right clicking → User Interface → Widget Blueprint → Selecting my class:
If I change the variable in the c++ to something else other than the demanded name, the blueprint fails to compile:
i.e.
class UTextBlock* CountdownEventTimerB;
Within Rider (The editor I use), I can see the blueprint class as the derived blueprint class, make it seem like everything is set correctly:
What am I doing wrong here? I feel as if I have been going in YouTube tutorial/UE forum circles with nothing working to perform this (seemingly simple) operation.