RealDocs

AActor::AddComponent

function Engine Blueprint Since 4.0
#include "GameFramework/Actor.h"
Access: public Specifiers: UFUNCTIONBlueprintCallable

Description

Blueprint-internal function that instantiates a component from a named template and optionally attaches it. This is the backend for the 'Add Component' Blueprint node and should not be called from game code.

Caveats & Gotchas

  • The header explicitly states 'DO NOT CALL MANUALLY — BLUEPRINT INTERNAL USE ONLY'. In C++ code, construct components with NewObject<UMyComponent>() in the constructor or CreateDefaultSubobject<>(), then call RegisterComponent() at runtime.
  • When bDeferredFinish is true, the component is not fully registered until FinishAddComponent() is called. Accessing the component's world-facing API before that point will produce undefined results.
  • The TemplateName must match a component template defined on the Blueprint class. Passing an incorrect name will return nullptr.

Signature

ENGINE_API UActorComponent* AddComponent(FName TemplateName, bool bManualAttachment, const FTransform& RelativeTransform, const UObject* ComponentTemplateContext, bool bDeferredFinish = false)

Parameters

Name Type Description Default
TemplateName FName Name of the component template to instantiate from.
bManualAttachment bool If false, automatically attaches to the root (or makes this component the root if it's the first). If true, you must attach manually.
RelativeTransform const FTransform& Transform relative to the attach parent (used when bManualAttachment is false).
ComponentTemplateContext const UObject* Optional BPGC to find the component template in. Defaults to this actor's class.
bDeferredFinish bool If true, skips the final registration step so expose-on-spawn properties can be set before FinishAddComponent is called. false

Return Type

UActorComponent*

Example

Correct C++ runtime component creation (do NOT use AddComponent) C++
// Do NOT use AddComponent() in C++ — use this pattern instead:
UStaticMeshComponent* NewMesh = NewObject<UStaticMeshComponent>(this, UStaticMeshComponent::StaticClass());
NewMesh->SetupAttachment(GetRootComponent());
NewMesh->RegisterComponent();
NewMesh->SetStaticMesh(MyMeshAsset);

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.