Search code examples
c++unreal-engine4

How to assign component name during run-time?


Hej,

In the Below code, I've assigned StaticMeshComponent name as "StaticMeshComponentCOMP1, StaticMeshComponentCOMP2". It is not very efficient for N component.

for (int32 i = 0; i < 6; i++)
{
  StaticMeshComponent[i] = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP1"));
  StaticMeshComponent[i] = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP2"));
// more code here
    }

So I've tried to assign StaticMeshComponent name as StaticMeshComponentCOMP with 'i' as below, but couldn't able to achieve the result.

for (int32 i = 0; i < 6; i++)
{
     StaticMeshComponent[i] = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("StaticMeshComponentCOMP" + '%i'));
}

thank you for your help in advance


Solution

  • You have to create your name as a separate FString variable. FString then has several append-functions, e.g. AppendInt().

    FString name = FString(TEXT("StaticMeshComponentCOMP"));
    name.AppendChar('-');
    name.AppendInt(i);