Download

How to change a text for the TextRenderActor object in my UE4 editor using C++?

Hi.

I added a TextRenderActor object in my UE4 editor. Now I try to change a text for this component using C++. I added the code below in my C++ class but no TextRenderActor class is recognized. What am I doing wrong?

TextRenderActor* MyTextRenderActor = FindObject(ANY_PACKAGE, TEXT(“TextRenderActor8”));

I changed my code. It looks this way right now:

#include “Engine/TextRenderActor.h”
#include “Components/TextRenderComponent.h”

[…]

ATextRenderActor* MyTextRenderActor = FindObject<ATextRenderActor>(ANY_PACKAGE, TEXT(“TextRenderActor”));
MyTextRenderActor->GetTextRender()->SetText(FText::FromString(TEXT(“Set Text”)));

But UE4 (4.26.2) crashes when I run my aplication. Do you have any sugestions?

I made it dynamically. It works fine:

class ATextRenderActor* MyTextRenderActor; // define this in your .h file

[…]

MyTextRenderActor = GetWorld()->SpawnActor<ATextRenderActor>(ATextRenderActor::StaticClass(), FVector(0.f, 100.f, 170.f), FRotator(90.f, 180.f, 0.f));
MyTextRenderActor->GetTextRender()->SetText(FText::FromString(TEXT(“Set Text”)));

I added the code in actor’s constructor and it works fine:

ATextRenderActor* MyText = FindObject<ATextRenderActor>(ANY_PACKAGE, TEXT("TextRenderActor1"));

if (MyText)
{
	MyText->GetTextRender()->SetText(FText::FromString(TEXT("Set Text Render Actor")));

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "OK");
}