# How to include (add) a Blueprint component to C++ Actor?

**URL:** <https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414>\
**Category:** C++\
**Tags:** actorcomponent, unreal-engine, UE4-26, Blueprint, question, CPP\
**Created:** [January 29, 2022, 8:16pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414 "2022-01-29T20:16:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![thenull3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thenull3](https://forums.unrealengine.com/u/thenull3)\
**Post date:** [January 29, 2022, 8:16pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/1 "2022-01-29T20:16:28Z")

</div>

There is a Blueprint Component “FlatComponent” based on ActorComponent, it needs to be somehow added to the C++ Actor, which is called “FlatActor”.

**How can I do it?**

I tried adding this to FlatActor.h:

```auto
UPROPERTY(VisibleAnywhere)
TSubclassOf<class UActorComponent> FlatCompClass;

UPROPERTY()
UActorComponent* FlatComp;

```

And I don’t know what to do next.

* * *
What i want to see: 

![comps](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/f/51fa6f047fc20344b690be8cec66fb45f7b89537.png)

---

<div class="post-metadata">

**Author:** ![Natalo77](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Natalo77](https://forums.unrealengine.com/u/Natalo77)\
**Post date:** [January 31, 2022, 8:47am UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/2 "2022-01-31T08:47:00Z")

</div>

Try looking at the source code for `ACharacter`. The constructor will show you how to properly initialize a component.

---

<div class="post-metadata">

**Author:** ![thenull3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thenull3](https://forums.unrealengine.com/u/thenull3)\
**Post date:** [January 31, 2022, 2:11pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/3 "2022-01-31T14:11:28Z")

</div>

```auto
ACharacter::ACharacter(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
	// Structure to hold one-time initialization
	struct FConstructorStatics
	{
		FName ID_Characters;
		FText NAME_Characters;
		FConstructorStatics()
			: ID_Characters(TEXT("Characters"))
			, NAME_Characters(NSLOCTEXT("SpriteCategory", "Characters", "Characters"))
		{
		}
	};
	static FConstructorStatics ConstructorStatics;

	// Character rotation only changes in Yaw, to prevent the capsule from changing orientation.
	// Ask the Controller for the full rotation if desired (ie for aiming).
	bUseControllerRotationPitch = false;
	bUseControllerRotationRoll = false;
	bUseControllerRotationYaw = true;

	CapsuleComponent = CreateDefaultSubobject<UCapsuleComponent>(ACharacter::CapsuleComponentName);
	CapsuleComponent->InitCapsuleSize(34.0f, 88.0f);
	CapsuleComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);

	CapsuleComponent->CanCharacterStepUpOn = ECB_No;
	CapsuleComponent->SetShouldUpdatePhysicsVolume(true);
	CapsuleComponent->SetCanEverAffectNavigation(false);
	CapsuleComponent->bDynamicObstacle = true;
	RootComponent = CapsuleComponent;

	bClientCheckEncroachmentOnNetUpdate = true;
	JumpKeyHoldTime = 0.0f;
	JumpMaxHoldTime = 0.0f;
    JumpMaxCount = 1;
    JumpCurrentCount = 0;
	JumpCurrentCountPreJump = 0;
    bWasJumping = false;

	AnimRootMotionTranslationScale = 1.0f;

#if WITH_EDITORONLY_DATA
	ArrowComponent = CreateEditorOnlyDefaultSubobject<UArrowComponent>(TEXT("Arrow"));
	if (ArrowComponent)
	{
		ArrowComponent->ArrowColor = FColor(150, 200, 255);
		ArrowComponent->bTreatAsASprite = true;
		ArrowComponent->SpriteInfo.Category = ConstructorStatics.ID_Characters;
		ArrowComponent->SpriteInfo.DisplayName = ConstructorStatics.NAME_Characters;
		ArrowComponent->SetupAttachment(CapsuleComponent);
		ArrowComponent->bIsScreenSizeScaled = true;
	}
#endif // WITH_EDITORONLY_DATA

	CharacterMovement = CreateDefaultSubobject<UCharacterMovementComponent>(ACharacter::CharacterMovementComponentName);
	if (CharacterMovement)
	{
		CharacterMovement->UpdatedComponent = CapsuleComponent;
		CrouchedEyeHeight = CharacterMovement->CrouchedHalfHeight * 0.80f;
	}

	Mesh = CreateOptionalDefaultSubobject<USkeletalMeshComponent>(ACharacter::MeshComponentName);
	if (Mesh)
	{
		Mesh->AlwaysLoadOnClient = true;
		Mesh->AlwaysLoadOnServer = true;
		Mesh->bOwnerNoSee = false;
		Mesh->VisibilityBasedAnimTickOption = EVisibilityBasedAnimTickOption::AlwaysTickPose;
		Mesh->bCastDynamicShadow = true;
		Mesh->bAffectDynamicIndirectLighting = true;
		Mesh->PrimaryComponentTick.TickGroup = TG_PrePhysics;
		Mesh->SetupAttachment(CapsuleComponent);
		static FName MeshCollisionProfileName(TEXT("CharacterMesh"));
		Mesh->SetCollisionProfileName(MeshCollisionProfileName);
		Mesh->SetGenerateOverlapEvents(false);
		Mesh->SetCanEverAffectNavigation(false);
	}

	BaseRotationOffset = FQuat::Identity;
}

```

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [January 31, 2022, 4:23pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/4 "2022-01-31T16:23:49Z")

</div>

static ConstructorHelpers::FClassFinder FlatCompClass(TEXT("/Game/YourAssetPath"));

---

<div class="post-metadata">

**Author:** ![thenull3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thenull3](https://forums.unrealengine.com/u/thenull3)\
**Post date:** [January 31, 2022, 6:09pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/5 "2022-01-31T18:09:40Z")

</div>

Well I wrote this:

```auto
static ConstructorHelpers::FClassFinder<UActorComponent> FlatCompOb(TEXT("Blueprint'/Game/Components/FlatComponent.FlatComponent_C'"));
	FlatCompClass = FlatCompOb.Class;

	UClass* baseClass = FindObject<UClass>(ANY_PACKAGE, TEXT("ActorComponent"));
	if (FlatCompClass->IsChildOf(baseClass))
	{
		UActorComponent* NewComp = NewObject<UActorComponent>(FlatCompClass);

		NewComp->RegisterComponent();
		
	}

```

The path is defined, but my FlatComponent is not added as a component.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/e/9ee9e316505943d5bfb50b54cd535a00f019f6c2.png)

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [January 31, 2022, 7:38pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/6 "2022-01-31T19:38:07Z")

</div>

Try using CreateDefaultSubobject instead.

---

<div class="post-metadata">

**Author:** ![thenull3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thenull3](https://forums.unrealengine.com/u/thenull3)\
**Post date:** [January 31, 2022, 9:21pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/7 "2022-01-31T21:21:21Z")

</div>

I tried but it didn’t work, can you explain how to make it work?

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/1/51128a8582c63dc5d04453d6b29775df118661e8.png)

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [February 1, 2022, 3:36am UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/8 "2022-02-01T03:36:20Z")

</div>

There is also one defined by the UObject class.

```auto
FlatComp = Cast<UActorComponent>(CreateDefaultSubobject(TEXT("Component Display Name"), FlatCompOb.Class, FlatCompOb.Class, false, false));

// Latter two booleans you should check what they do, funnily enough the C++ API docs on the ConstructorHelper's side makes it more clear while the UObject one is not commented.

```

> **[UObject](https://docs.unrealengine.com/4.26/en-US/API/Runtime/CoreUObject/UObject/UObject/)**
>
> The base class of all UE4 objects.

> **[FObjectInitializer](https://docs.unrealengine.com/4.26/en-US/API/Runtime/CoreUObject/UObject/FObjectInitializer/)**
>
> Internal class to finalize \[UObject\](API\\Runtime\\CoreUObject\\UObject\\UObject) creation (initialize properties) after the real C++ constructor is called.

---

<div class="post-metadata">

**Author:** ![thenull3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@thenull3](https://forums.unrealengine.com/u/thenull3)\
**Post date:** [February 1, 2022, 3:02pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/9 "2022-02-01T15:02:27Z")

</div>

Thanks, it works!  
But there is a problem: in order to use my FlatComponent in Blueprints, I need to use Cast To FlatComponent to edit it.

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/2/ed2a483352878753b3e3aba057f07f773fb487e1.png)

And is it possible to get variables from my Component in C++?  
If so, how?

---

<div class="post-metadata">

**Author:** ![JoSf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JoSf](https://forums.unrealengine.com/u/JoSf)\
**Post date:** [February 1, 2022, 3:13pm UTC](https://forums.unrealengine.com/t/how-to-include-add-a-blueprint-component-to-c-actor/492414/10 "2022-02-01T15:13:59Z")

</div>

Well… it’s inheritance 101 - your “flat component” is both an UActorComponent and UFlatComponent. But in your header, you’ve defined the pointer as UActorComponent\*.

So instead of declaring FlatComp as UActorComponent\*, declare it as UFlatComponent\*. At this point you might want to check on forward declaring so you don’t need to add any includes in the header file, but rather can add them to the .cpp file.
