컴포넌트를 만들어 액터에 붙이는 시점은 아래 세 부분 정도로 나눠 볼 수 있다.
1. class construction 과정
2. blueprint construction 과정
3. beginplay 혹은 그 이후 과정
이때 대부분은 엔진 내 블루프린트 뷰에서 컴포넌트를 확인하기 위해 actor의 class construction 과정에서
'CreateDefaultSubobject' 함수를 통해 생성하고 초기화한다.
이런 경우, ActorConstruction (BeginPlay나 OnConstruction 이전)에서 Actor 내 Component를 등록하고 초기화한다.
하지만 ActorConstruction 이후 즉 BeginPlay나 OnConstruction에서 컴포넌트를 생성하는 경우 컴포넌트 등록과 초기화를 수동으로 해줘야 한다.
따라서, 컴포넌트를 동적으로 만들어야하 하는 경우, Runtime Created Components 와 같은 명의 동적 컴포넌트 관리 자료구조를 두고 Manual 한 초기화를 해주는 것이 좋다.
간략하게 보면 아래와 같을 것 이다.
void RegisterRuntimeCreatedComponents()
{
for (auto& RuntimeComp : UnregisteredRuntimeComponents)
{
if (RuntimeComp.Get())
{
RuntimeComp->RegisterComponent();
if (RuntimeComp->HasBeenInitialized() == false)
{
RuntimeComp->InitializeComponent();
}
}
}
}