What is the correct way to create and add components at runtime ?

I’m using Unreal5, but what i’ve found is the AddComponentByClass();

AddComponentByClass(TSubclassOf<UActorComponent> Class, bool bManualAttachment, const FTransform& RelativeTransform, bool bDeferredFinish)

and for example:

AddComponentByClass(UMyComponent::StaticClass, false, SpawnTransform, false);

This uses the NewObject method and will register your component.

7 Likes

I’m trying to convert a CreateComponentByClass Blueprint Node to C++, I‘m not sure if it’s appropriate. Because the blueprint Node only have two Pin.