# Replace Root Component on an Inherited Class

**URL:** <https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573>\
**Category:** C++\
**Tags:** root-component, unreal-engine, CPP, question\
**Created:** [December 17, 2016, 9:57pm UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573 "2016-12-17T21:57:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![poplockentaco](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@poplockentaco](https://forums.unrealengine.com/u/poplockentaco)\
**Post date:** [December 17, 2016, 9:57pm UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/1 "2016-12-17T21:57:31Z")

</div>

Hi everyone!

So my question is: is it possible to replace the root component in a subclass if I’ve already changed it in the super class?

To explain more, I have a c++ class inheriting from Actor called BaseInteractable. I made the root component of BaseInteractable a SphereComponent. Now in a subclass of BaseInteractable, BaseInteractable\_Child, I am trying to change the root component to a BoxComponent. But I am having issues with it. Does anyone know if a) this is possible to do and b) how I might do this?

Any help or advice is greatly appreciated thanks!

---

<div class="post-metadata">

**Author:** ![poplockentaco](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@poplockentaco](https://forums.unrealengine.com/u/poplockentaco)\
**Post date:** [December 17, 2016, 10:34pm UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/2 "2016-12-17T22:34:28Z")

</div>

Alright never mind haha. It’s always right after you think you can’t do something that you find the solution.

So apparently the solution was very simple:

SuperClass.h

```
// Declare a sphere component 
UPROPERTY(VisibleDefaultsOnly, Category = Interact)
class USphereComponent* InteractArea;

```

SuperClass.cpp

```
// Create subobject in the constructor
InteractArea = CreateDefaultSubobject<USphereComponent>(TEXT("InteactArea"));
RootComponent = InteractArea;

```

SubClass.h

```
// Declare a new component to use as the new root component
UPROPERTY(VisibleDefaultsOnly, Category = Interact)
class UStaticMeshComponent* SM;

```

SubClass.cpp

```
// Create the subobject make it the new root component
SM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SM"));

// Note that this makes SM the new root but it then attaches the previous root to SM
RootComponent = SM;

/* 
 * The above code makes SM the new root as mentioned, but then it effectually does:
 * InteractArea->SetupAttachment(GetRootComponent());
 * 
 * At least that's what I'm observing. It could be doing something else.
 */

```

This is the behavior I was going for, but hopefully others will find this helpful too.

---

<div class="post-metadata">

**Author:** ![Napoleonite](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Napoleonite](https://forums.unrealengine.com/u/Napoleonite)\
**Post date:** [January 18, 2018, 5:46pm UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/3 "2018-01-18T17:46:36Z")

</div>

Yes you have to assign a new RootComponent and then call:

```
InteractArea->DestroyComponent(); // Unregisters and destroys the old RootComponent

```

Otherwise the old component will still exist and will still interfere with the gameplay which may not always be intended.

---

<div class="post-metadata">

**Author:** ![imtrobin2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@imtrobin2](https://forums.unrealengine.com/u/imtrobin2)\
**Post date:** [May 25, 2024, 2:02am UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/4 "2024-05-25T02:02:54Z")

</div>

This works but is there a way to change it from blueprint? I tried construction node, didn’t work. I also tried setting a C++ variable to choose which root but the ctor doesn’t read the BP variable.

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [May 25, 2024, 10:36am UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/5 "2024-05-25T10:36:36Z")

</div>

Looked a little bit into it,  
if you are in C++ there is an easier way to change a component the important part is to set the component defined in the parent of a common root type of the new component.

e.g. if you want your class to support box and sphere collider I would recommend to use the next common parent which is a primitive component.

Then you would assign the component with a fixed name and can override it in the constructor in the C++ class.

TestActor.h

```auto
#pragma once

#include "GameFramework/Actor.h"

#include "TestActor.generated.h"

UCLASS(Blueprintable)
class PROPERTYGRAPHII_API ATestActor : public AActor
{
	GENERATED_BODY()

public:
	static const FName RootPrimitiveComponentName;
	
	explicit ATestActor(const FObjectInitializer& ObjectInitializer);
	
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	TObjectPtr<UPrimitiveComponent> RootPrimitiveComponent { nullptr };
};

UCLASS(Blueprintable)
class PROPERTYGRAPHII_API AChildTestActor : public ATestActor
{
	GENERATED_BODY()

public:
	explicit AChildTestActor(const FObjectInitializer& ObjectInitializer);
};

```

TestActor.cpp

```auto

#include "PropertyGraphII/TestActor.h"

#include "Components/BoxComponent.h"
#include "Components/SphereComponent.h"

const FName ATestActor::RootPrimitiveComponentName = TEXT("Root");

ATestActor::ATestActor(const FObjectInitializer& ObjectInitializer)
{
	RootPrimitiveComponent = Cast<UPrimitiveComponent>(CreateDefaultSubobject(RootPrimitiveComponentName, UPrimitiveComponent::StaticClass(),
		USphereComponent::StaticClass(), true, false));
	SetRootComponent(RootPrimitiveComponent);
}

AChildTestActor::AChildTestActor(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UBoxComponent>(ATestActor::RootPrimitiveComponentName))
{
}

```

The important part is

```auto
Super(ObjectInitializer.SetDefaultSubobjectClass<UBoxComponent>(ATestActor::RootPrimitiveComponentName))

```

which defines the new type for the component identified with ATestActor::RootPrimitiveComponentName by name.

* * *

Now to blueprint.  
I couldn’t find an easy way how to do this in blueprint only the best thing I could find was to create a base C++ class with a defined component then you are able to change the class of the component in blueprints.

ABlueprintTestActor.h

```auto
#pragma once

#include "GameFramework/Actor.h"

#include "BlueprintTestActor.generated.h"

UCLASS(Blueprintable)
class PROPERTYGRAPHII_API ABlueprintTestActor : public AActor
{
	GENERATED_BODY()

public:
	static const FName OverridableRootComponentName;
	
	explicit ABlueprintTestActor(const FObjectInitializer& ObjectInitializer);
	
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	TObjectPtr<USceneComponent> OverridableRootComponent { nullptr };
};

```

ABlueprintTestActor.cpp

```auto
#include "PropertyGraphII/BlueprintTestActor.h"

const FName ABlueprintTestActor::OverridableRootComponentName = TEXT("Root");

ABlueprintTestActor::ABlueprintTestActor(const FObjectInitializer& ObjectInitializer)
{
	OverridableRootComponent = CreateDefaultSubobject<USceneComponent>(OverridableRootComponentName);
	SetRootComponent(OverridableRootComponent);
}

```

This allows you to change the component in blueprints by selecting a different component class.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/5/8/e5885d7ab4c77dfa047408a10f4fdb14d94467c7.png)

The only issue that I found with this is that child blueprints can only go into more specialization.  
Meaning that if you change the component to a sphere component you can only change the component to something even more specific in the child.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/b/9ebc8e05156c85f5e803cfe138ce6d3ed0d0940d.png)  
sphere component can get more specialized to a DrawSphereComponent but not to a box component.

So guess the lesson here is keep the root on a common type until you specialize.

---

<div class="post-metadata">

**Author:** ![imtrobin2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@imtrobin2](https://forums.unrealengine.com/u/imtrobin2)\
**Post date:** [June 2, 2024, 2:58pm UTC](https://forums.unrealengine.com/t/replace-root-component-on-an-inherited-class/379573/7 "2024-06-02T14:58:52Z")

</div>

Thanks, I think this is almost perfect
