Change the text value for a TextRenderActor

Hi;
I have a TextRenderActor in and I would like to change the text value.
So far I’m able to get the list of actors in the world and I getting my TextRenderActor. How to go about changing the text for the TextRenderActor.
Here is the code I have so far:

void AMyActor::PrintAllActorsLocations()
{
//EngineUtils.h
for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
// check for render text actor
if (ActorItr->GetName().Equals(TEXT(“TextRenderActor_1”), ESearchCase::CaseSensitive)) {
UE_LOG(YourLog, Warning, TEXT(“The String! %s”), *ActorItr->GetName());
// Change the Text Render Actor???
}
}
}

Thank you for your help.

Hello and welcome to the forums.

I’m going to ignore the fact that checking for a specific actor using its hardcoded name is probably not the best idea.
With your current setup this would be how to change the text:
First you have to cast the AActor you’re getting from your TActorIterator to ATextRenderActor.
Then you’d check whether the cast succeeded or not by comparing the result of the cast to NULL/nullptr.
If that worked you can use the pointer to call GetTextRender() which returns a UTextRenderComponent*.
Again check whether this pointer is not NULL/nullptr.
If this was successful again you can finally use the UTextRenderComponent to access and change the Text UPROPERTY. But do not change the property manually, use the SetText function instead!


ATextRenderActor* TextRenderActor = Cast<ATextRenderActor>(*ActorItr);
if (TextRenderActor)
{
	UTextRenderComponent* TextRenderComponent = TextRenderActor->GetTextRender();
	if (TextRenderComponent)
	{
		TextRenderComponent->SetText(/*YOUR TEXT AS FText HERE*/);
	}
	else
	{
		//failure handling (TextRenderActor didn't have a valid/non-NULL TextRenderComponent)
	}
}
else
{
	//failure handling (cast failed)
}

If you’d use TActorIterator<ATextRenderActor> instead, you’d no longer need to cast and can work directly with the iterator.

NOTE: the above code hasn’t been compiled or tested :slight_smile:

Thank you for the reply, but this what I ended up with.

void AMyActor::PrintAllActorsLocations(FString msg)
{
//EngineUtils.h
for (TActorIterator<AActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
// check for render text actor
if (ActorItr->GetName().Equals(TEXT(“TextRenderActor_1”), ESearchCase::CaseSensitive)) {
UE_LOG(YourLog, Warning, TEXT(“The String! %s”), ActorItr->GetName());
UTextRenderComponent
TextRender = (UTextRenderComponent *) ActorItr->GetRootComponent();
TextRender->SetText(msg);

	}
}

}