Why is UTextRenderComponent just a solid box?

246717-ue4q.png

.h

UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
UTextRenderComponent* TRC;

.cpp

USquareComponent::USquareComponent()
{
	ConstructorHelpers::FObjectFinder<UStaticMesh> BasicPlane(TEXT("StaticMesh'/Engine/BasicShapes/Plane.Plane'"));
	ConstructorHelpers::FObjectFinder<UFont> TextRenderFont(TEXT("Font'/Game/AuraTD/Fonts/Aldrich-Regular_Font.Aldrich-Regular_Font'"));

	PlaneSMC = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Plane"));
	PlaneSMC->SetupAttachment(this);
	PlaneSMC->SetCastShadow(false);

	if (BasicPlane.Succeeded())
	{
		PlaneSMC->SetStaticMesh(BasicPlane.Object);
		PlaneSMC->OnBeginCursorOver.AddDynamic(this, &USquareComponent::OnMouseOver);
		PlaneSMC->SetRelativeScale3D(FVector(0.94f));
		PlaneSMC->SetRelativeLocation(FVector(0.f, 0.f, 0.5f));
	}

	TRC = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Id"));
	TRC->SetupAttachment(PlaneSMC);
	TRC->SetRelativeRotation(FRotator(90.f, 0.f, 180.f));
	TRC->SetRelativeLocation(FVector(0.f, 0.f, 1.f));
	TRC->SetHorizontalAlignment(EHorizTextAligment::EHTA_Center);

	if (TextRenderFont.Succeeded())
	{
		TRC->SetFont(TextRenderFont.Object);
		TRC->SetTextRenderColor(FColor::Black);
	}
}

If I comment out TRC->SetFont(TextRenderFont.Object); then I get actual text. I also tried offline fonts from 10px up to 72px.

Apparently, the font also needs a material. The following material has parameters that can be changed at runtime. For that you need a dynamic material instance. You can look into that if you’re interested. For now, we’re just gonna slap the material on our font:



In the editor, right click the material > copy reference. Use this path for your .cpp constructor.

 #include "ConstructorHelpers.h"

 ConstructorHelpers::FObjectFinder<UMaterial> FontMaterial(TEXT("Material'/Game/AuraTD/Fonts/AldrichMaterial.AldrichMaterial'"));

I then put the Succeeded() check inside my font asset check. If the font doesn’t load, there’s no reason to do anything with the material. TRC is of course my text render component.

if (TextRenderFont.Succeeded())
{
	TRC->SetFont(TextRenderFont.Object);
	TRC->SetTextRenderColor(FColor::Blue);

	if (FontMaterial.Succeeded())
	{
		TRC->SetTextMaterial(FontMaterial.Object);
	}
}