# Ways to "Add Component from class"?

**URL:** <https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [August 6, 2015, 7:22am UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830 "2015-08-06T07:22:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hayaweh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hayaweh](https://forums.unrealengine.com/u/Hayaweh)\
**Post date:** [August 6, 2015, 7:22am UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/1 "2015-08-06T07:22:24Z")

</div>

Hi,

I am searching for some hours now. But I find nothing so maybe some of you guys will know that. 🙂

Is there any way to add a component in the same manner as we can “Spawn actor from class” by using classes?

I am currently having some blueprints derived from ActorComponent that have each different behaviors (base bloc definition and implementation + all childs with specialized behaviors).

As such. I am having a build tool that is adding new blocs to the “vehicle actor” and also deleting them (I do internal tracking).

But it seems like there is no way to have anything dynamic when it comes to Components like spawning from class or anything like that that would avoid me using something like an enum and then using the “Add Metal Bloc Component” or “Add Thruster Bloc Component” etc etc etc.

I started also digging into C++ code to maybe achieve this using TSubClassOf but I just keep having warnings such as "Static class is not part of TSubClassOf etc. (I could post the exact thing if anybody wants to know).

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![intoxicat3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@intoxicat3](https://forums.unrealengine.com/u/intoxicat3)\
**Post date:** [October 5, 2015, 4:54pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/2 "2015-10-05T16:54:55Z")

</div>

I have the same problem.

---

<div class="post-metadata">

**Author:** ![Hayaweh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hayaweh](https://forums.unrealengine.com/u/Hayaweh)\
**Post date:** [October 5, 2015, 6:01pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/3 "2015-10-05T18:01:59Z")

</div>

Hey Intoxicat3,

as I encountered the issue, I did end up writting myself a very simple function that allows me to add new ActorComponents in C++.

You can put that in a class deriving from AActor and use it as a base to create the actors that will have the possibility to add new components.

This function does not expose the defaults parameters but it works. 🙂

**.h**

```
UFUNCTION(BlueprintCallable, Category = "Component|AddComponent")
	UActorComponent* AddActorComponent(UClass* ActorComponentClass);

```

**.cpp**

```
UActorComponent* APhysicsActorCode::AddActorComponent(UClass* ActorComponentClass)
{
	UClass * baseClass = FindObject<UClass>(ANY_PACKAGE, TEXT("ActorComponent"));
	if (ActorComponentClass->IsChildOf(baseClass))
	{
		UActorComponent* NewComp = NewObject<UActorComponent>(this, ActorComponentClass);
		if (!NewComp)
		{
			return NULL;
		}
		// ~~~~~~~~~~~~~

		NewComp->RegisterComponent(); //You must ConstructObject with a valid Outer that has world, see above	 

		return NewComp;
	}
	else
		return NULL;
}

```

I am using it for my project until UE4 supports it natively.

Tell me if this works or if you need any other indications.

Erio

---

<div class="post-metadata">

**Author:** ![intoxicat3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@intoxicat3](https://forums.unrealengine.com/u/intoxicat3)\
**Post date:** [October 5, 2015, 7:13pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/4 "2015-10-05T19:13:52Z")

</div>

Hi Erio - thanks for this!

…but I need to have it working in blueprints and binary version of the engine. Hope Epic will add it in 4.10 as “polishing current features” feature 😉

---

<div class="post-metadata">

**Author:** ![Hayaweh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hayaweh](https://forums.unrealengine.com/u/Hayaweh)\
**Post date:** [October 5, 2015, 7:50pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/5 "2015-10-05T19:50:56Z")

</div>

As you can notice, with the UFUNCTION line, this in fact creates a node you can call in Blueprint. 😉

EDIT: It also works with the binary version of the engine… You just have a little trick to do:

- Create a C++ class inheriting AActor
- Add the code I gave you, changing the name of the class
- Compile
- In UE4 Editor, for the actors you want to have the ability to add actor components, make their parent the new class you created.

In fact, instead of making a BP from an Actor, you will make a BP from the class you created, which is an actor but with the enhanced C++ code. 😉

---

<div class="post-metadata">

**Author:** ![DizietSma](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DizietSma](https://forums.unrealengine.com/u/DizietSma)\
**Post date:** [February 13, 2018, 10:15am UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/6 "2018-02-13T10:15:06Z")

</div>

Hello!  
I’m not much into C++

Can you please show full .h and .cpp solution?

---

<div class="post-metadata">

**Author:** ![BenVolo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BenVolo](https://forums.unrealengine.com/u/BenVolo)\
**Post date:** [October 16, 2020, 8:48am UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/7 "2020-10-16T08:48:24Z")

</div>

I also don’t understand how to implement this.

---

<div class="post-metadata">

**Author:** ![Hayaweh](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Hayaweh](https://forums.unrealengine.com/u/Hayaweh)\
**Post date:** [October 16, 2020, 11:09pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/8 "2020-10-16T23:09:39Z")

</div>

Here’s an updated version working with 4.25, in a blueprint function library:  
.h

```
#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ComponentHelperLibrary.generated.h"

/**
 * 
 */
UCLASS()
class ADVANCEDLOCOMOTIONSYSTEMV_API UComponentHelperLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable, Category = "Components|AddComponent")
	static UActorComponent* AddActorComponent(AActor* Owner, TSubclassOf<UActorComponent> ActorComponentClass);
};

```

.cpp

```
#include "ComponentHelperLibrary.h"

UActorComponent* UComponentHelperLibrary::AddActorComponent(AActor* Owner, TSubclassOf<UActorComponent> ActorComponentClass)
{
    UClass * baseClass = FindObject<UClass>(ANY_PACKAGE, TEXT("ActorComponent"));
    if (ActorComponentClass->IsChildOf(baseClass))
    {
        UActorComponent* NewComp = NewObject<UActorComponent>(Owner, ActorComponentClass);
        if (!NewComp)
        {
            return NULL;
        }
        // ~~~~~~~~~~~~~
 
        NewComp->RegisterComponent(); //You must ConstructObject with a valid Outer that has world, see above     
 
        return NewComp;
    }
    else
        return NULL;
}
```

---

<div class="post-metadata">

**Author:** ![BenVolo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BenVolo](https://forums.unrealengine.com/u/BenVolo)\
**Post date:** [October 25, 2020, 9:55pm UTC](https://forums.unrealengine.com/t/ways-to-add-component-from-class/327830/9 "2020-10-25T21:55:41Z")

</div>

Some slightly more transparent instructions based on Hayaweh’s method if you are a noob like me. (thanks again so much for your help)

Open your project and create a new C++ Class. when selecting which class it inherits from, choose Blueprint Function Library. Make note of the name of this class and replace ###INSERTCLASSNAMEHERE### below with the class name while leaving the U before it. The proper includes and formatting will be auto generated by unreal, all you have to do is copy and paste the following at the line specified and hit rebuild.

//INSERT THIS AFTER GENERATED\_BODY() in .h file:

```
  public:
      UFUNCTION(BlueprintCallable, Category = "Components|AddComponent")
      static UActorComponent* AddActorComponent(AActor* Owner, TSubclassOf<UActorComponent> ActorComponentClass);

```

//INSERT THIS AFTER INCLUDE in .cpp file:

```
 UActorComponent* U###INSERTCLASSNAMEHERE###::AddActorComponent(AActor* Owner, TSubclassOf<UActorComponent> ActorComponentClass)
  {
      UClass * baseClass = FindObject<UClass>(ANY_PACKAGE, TEXT("ActorComponent"));
      if (ActorComponentClass->IsChildOf(baseClass))
      {
          UActorComponent* NewComp = NewObject<UActorComponent>(Owner, ActorComponentClass);
          if (!NewComp)
          {
              return NULL;
          }
          // ~~~~~~~~~~~~~
   
          NewComp->RegisterComponent(); //You must ConstructObject with a valid Outer that has world, see above     
   
          return NewComp;
      }
      else
          return NULL;
  }

```

Ensure that everything compiles properly, save, and exit out of and then reopen Unreal and you will see it in your blueprints. It will not show up in blueprints until you restart unreal.
