dawnarc
(dawnarc)
August 12, 2016, 6:24pm
1
I want to create a Text Render Component and attach it to floor at runtime. following is my test code, but it doesn’t work:
void ATopDownTestGameMode::BeginPlay()
{
Super::BeginPlay();
const TArray<AActor*> ActorList = GetWorld()->GetLevel(0)->Actors;
const FString MapName("Floor");
AStaticMeshActor* Floor = NULL;
for (int i = 0, size = ActorList.Num(); i < size; i++)
{
if (!ActorList*)
{
continue;
}
if (MapName.Equals(ActorList*->GetName()))
{
Floor = Cast<AStaticMeshActor>(ActorList*);
break;
}
}
if (Floor)
{
UTextRenderComponent* Text = NewObject<UTextRenderComponent>(this);
Text->SetRelativeLocation(FVector(-282.f, 100, 100.f));
Text->SetTextRenderColor(FColor::Red);
Text->SetText(FText::FromString(TEXT("Dynamically added TextRenderComponent does not work")));
Text->SetXScale(1.f);
Text->SetYScale(1.f);
Text->SetWorldSize(500);
Text->AttachToComponent(Cast<USceneComponent>(Floor), FAttachmentTransformRules::KeepRelativeTransform);
}
}
TheJamsh
(Jambax)
August 13, 2016, 7:58am
2
You need to call:
Text->RegisterComponent();
Do it after you call NewObject<>
dawnarc
(dawnarc)
August 13, 2016, 9:59am
3
Thx for your suggestion, but it doesn’t work.
dawnarc
(dawnarc)
August 15, 2016, 10:43am
4
Can anyone help me to solve this issue?
pickersZ
(pickersZ)
August 15, 2016, 12:13pm
5
Just a guess! try shifting “UTextRenderComponent* Text” to header file.
As mentioned by TheJamsh, add “Text->RegisterComponent();” immediately after newobject.
You might need to do printf or something to check the value of floor before if(floor) statement. It might bypass the creation process.
dawnarc
(dawnarc)
August 15, 2016, 12:49pm
6
It doesn’t work, Thx all the same
rotwang
(rotwang)
August 15, 2016, 12:55pm
7
You could use ATextRenderActor
dawnarc
(dawnarc)
August 15, 2016, 2:49pm
8
Thx so much, your suggestion is very useful. I worked it out:
void ATopDownTestGameMode::BeginPlay()
{
Super::BeginPlay();
Text = GetWorld()->SpawnActor<ATextRenderActor>(ATextRenderActor::StaticClass(), FVector(0.f, 100, 170.f), FRotator(90.f, 180.f, 0.f));
Text->GetTextRender()->SetText(FText::FromString(TEXT("Dynamic Text")));
Text->GetTextRender()->SetTextRenderColor(FColor::Red);
Text->SetActorScale3D(FVector(5.f, 5.f, 5.f));
}
1 Like