# How do you add components to an actor class at runtime in C++?

**URL:** <https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 22, 2014, 12:04am UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796 "2014-03-22T00:04:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vampatori](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vampatori](https://forums.unrealengine.com/u/vampatori)\
**Post date:** [March 22, 2014, 12:04am UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/1 "2014-03-22T00:04:51Z")

</div>

I have my own actor class and a series of ‘building block’ blueprints which I want to add to my actor to build it up based on user input. I think AddComponent is the method I want, however I’m not having much luck getting it to work.

`FName TemplateName` - I’m not sure what the format of this should be. Should it just be “/Game/Path/BlueprintName”?

`UObject * ComponentTemplateContext` - I have no idea what this should be. It defaults to the actor class I’m calling this from I think, but I get the impression this should be the class of the blueprint I’m adding… if so, then how do I get that? At the moment I’m just passing NULL.

It runs without error, but nothing appears to happen. No matter what I put for the TemplateName, no error is produced, which leads me to think that it’s just failing silently.

Is this the right approach at all? Does anyone have a working example of doing this sort of thing?

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 22, 2014, 2:14am UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/2 "2014-03-22T02:14:45Z")

</div>

Why are you needing to add components to a class at runtime?

Rama

---

<div class="post-metadata">

**Author:** ![vampatori](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@vampatori](https://forums.unrealengine.com/u/vampatori)\
**Post date:** [March 22, 2014, 8:18am UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/3 "2014-03-22T08:18:54Z")

</div>

My ultimate goal is to allow my players to create content in the game from a series of building blocks (specifically space ships a the moment). So I have some building block blueprints that include meshes, lights, interactive elements with Blueprint logic, etc.

The player will create then will be able to pilot the vehicle - so I want to be able to apply forces to the vehicle as a whole. I also want to create further building blocks without going into C++ code, and specify which building blocks are available to a player at runtime.

Maybe I need to spawn and parent them?

---

<div class="post-metadata">

**Author:** ![Bob\_Gneu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Bob\_Gneu](https://forums.unrealengine.com/u/Bob_Gneu)\
**Post date:** [March 25, 2014, 10:32pm UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/4 "2014-03-25T22:32:26Z")

</div>

Components.Add was available during the beta. It may still be the case. Something along those lines.

Otherwise, look at how the pawn has its movement component set.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 25, 2014, 10:54pm UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/5 "2014-03-25T22:54:30Z")

</div>

#Attach

What you CAN easily do

is spawn Static Mesh Actors

and Attach them to your core ship component that is always created.

Then you can go wild with creating Static Mesh assets and then you can dynamically set the Static Mesh Actor’s Static mesh in code

#Set Static Mesh

//AStaticMeshActor\* MySMA in .h  
if(!MySMA) return;  
MySMA-\>StaticMeshComponent-\>SetStaticMesh(your static mesh)

#Dynamic Load Object

See my wiki page to load your various static mesh assets from file path

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [March 25, 2014, 10:57pm UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/6 "2014-03-25T22:57:04Z")

</div>

#Attach

You can create Static Mesh Actors and attach them to a core invisible actor that is the ships’ origin point

* * *

**Then they will move, rotate, AND SCALE with the core component**

* * *

so you just move around and scale the core actor, and all attached (at runtime) static mesh actors will follow just as you want

* * *

It’s easy!

* * *

#Attach, Actor.h

#Use EAttachLocation::KeepWorldPosition

```
/**
 * Attaches the RootComponent of this Actor to the RootComponent of the supplied actor, optionally at a named socket.
 * @param InParentActor Actor to attach this actor's RootComponent to
 * @param InSocketName Socket name to attach to, if any
 * @param AttachLocationType	Type of attachment, AbsoluteWorld to keep its world position, RelativeOffset to keep the object's relative offset and SnapTo to snap to the new parent.
 */

UFUNCTION(BlueprintCallable, meta=(FriendlyName = "AttachActorToActor", AttachLocationType="KeepRelativeOffset"), Category="Utilities|Orientation")

void AttachRootComponentToActor(AActor* InParentActor, FName InSocketName = NAME_None, EAttachLocation::Type AttachLocationType = EAttachLocation::KeepRelativeOffset);

```

#Set Static Mesh

Spawn your static mesh actor (AStaticMeshActor\* MySMA)

```
if(!MySMA)return;
MySMA->StaticMeshComponent->SetStaticMesh(YourSMAsset)

```

#Dynamic Load Object

```
UStaticMesh* YourSMAsset = LoadedObjectFromFilePath

```

**For rest of details see my wiki page**

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

#Implementation

- create lots of static mesh assets
- create ONE blueprint of the static mesh actor class
- spawn this blueprint at runtime
- set the static mesh asset from file path for this spawned SMA

so you have

One Static Mesh Actor Blueprint = ShipPartBP  
and looots of actual static mesh assets

and you attach the spawned Ship Parts to the invisible ship Core Actor

and then only move / rotate / scale the core ship actor

Rama

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_7f3a8c431](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_7f3a8c431](https://forums.unrealengine.com/u/anonymous_user_7f3a8c431)\
**Post date:** [June 11, 2014, 3:54pm UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/7 "2014-06-11T15:54:56Z")

</div>

You are amazing. Thank you for all your input.

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [June 11, 2014, 8:46pm UTC](https://forums.unrealengine.com/t/how-do-you-add-components-to-an-actor-class-at-runtime-in-c/277796/8 "2014-06-11T20:46:24Z")

</div>

You’re welcome!

🙂

Rama
