void ACountdown::UpdateTimerDisplay()
{
CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
}
And,
void ACountdown::CountdownHasFinished()
{
CountdownText->SetText(TEXT("Go!"));
}
This is an example of a countdown where the text 3, 2, 1 Go! is printed after the game starts. However...Error message were printed in the two parts above. (FString::FromInt and TEXT)
The error message is as follows.
Error (active) E0312 no suitable user-defined conversion from "FString" to "const FText" exists
Error (active) E0415 no suitable constructor exists to convert from "const wchar_t [4]" to "FText"
Error C2664 'void UTextRenderComponent::SetText(const FText &)': cannot convert argument 1 from 'FString' to 'const FText &'
Error C2664 'void UTextRenderComponent::SetText(const FText &)': cannot convert argument 1 from 'const wchar_t [4]' to 'const FText &'
I wrote the whole code at the bottom.
I was following the example below.
I practiced the examples of Variables, Timers, and Events below.
(https://docs.unrealengine.com/5.0/en-US/quick-start-guide-to-variables-timers-and-events-in-unreal-engine-cpp/)
Did I forget about the header file? I'm at Countdown.cpp
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/TextRenderComponent.h"
#include "Countdown.generated.h"
at Countdown.h
#include "Components/TextRenderComponent.h"
#include "Countdown.h"
I'm just guessing there's something missing in the notation of the string. I rewrote the code more than five times because I'm afraid I made a mistake. But nothing has changed. Is there something that works on UE4 but not on UE5? How can I fix this?
Thank you for reading it.
it is Countdown.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/TextRenderComponent.h"
#include "Countdown.generated.h"
UCLASS()
class MYPROJECT_API ACountdown : public AActor
{
GENERATED_BODY()
public:
ACountdown();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
int32 CountdownTime;
UTextRenderComponent* CountdownText;
void UpdateTimerDisplay();
void AdvanceTimer();
void CountdownHasFinished();
FTimerHandle CountdownTimerHandle;
};
and Countdown.cpp #pragma once
#include "Components/TextRenderComponent.h"
#include "Countdown.h"
ACountdown::ACountdown()
{
PrimaryActorTick.bCanEverTick = false;
CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
CountdownText->SetHorizontalAlignment(EHTA_Center);
CountdownText->SetWorldSize(150.0f);
RootComponent = CountdownText;
CountdownTime = 3;
}
void ACountdown::BeginPlay()
{
Super::BeginPlay();
UpdateTimerDisplay();
GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);
}
void ACountdown::UpdateTimerDisplay()
{
CountdownText->SetText(FString::FromInt(FMath::Max(CountdownTime, 0)));
}
void ACountdown::AdvanceTimer()
{
--CountdownTime;
UpdateTimerDisplay();
if (CountdownTime < 1)
{
GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
CountdownHasFinished();
}
}
void ACountdown::CountdownHasFinished()
{
CountdownText->SetText(TEXT("Go!"));
}