# Replace a C++ Native Component with a Derived class

**URL:** <https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230>\
**Category:** C++\
**Tags:** unreal-engine, Blueprint\
**Created:** [November 23, 2021, 12:19am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230 "2021-11-23T00:19:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDSRS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MDSRS](https://forums.unrealengine.com/u/MDSRS)\
**Post date:** [November 23, 2021, 12:19am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/1 "2021-11-23T00:19:31Z")

</div>

Hey, I’m trying to figure out how on earth to solve this gracefully -

I need to replace a Native Component with a Blueprint derived class.

Lets say we have:

```auto
UCLASS()
class Foo : public AActor {
    ...

    UPROPERTY(EditAnywhere)
    UBarComponent* ReplaceableComponent;
    ...
    public Foo() {
        ReplaceableComponent= CreateDefaultSubobject< UBarComponent>( TEXT( "ReplaceableComponent" ) );
    }
}

```

Now, if I blueprint my class Foo, in the left hand pane, I can see my Native Component registered.  
But I want to replace this instance, for this blueprint only.

Say I have created another Blueprint based off of UBarComponent called BP\_MyBarComponent.  
The blueprint editor will not let me replace “ReplaceableComponent” with an instance of BP\_MyBarComponent.

Has anyone figured out how to deal with this?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [November 23, 2021, 12:07pm UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/2 "2021-11-23T12:07:30Z")

</div>

You can’t replace them with Blueprint classes. The best you can do for that is expose a variable which you set in your Blueprint construction script or similar.

---

<div class="post-metadata">

**Author:** ![MDSRS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MDSRS](https://forums.unrealengine.com/u/MDSRS)\
**Post date:** [November 23, 2021, 11:31pm UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/3 "2021-11-23T23:31:41Z")

</div>

This seems to me like an oversight, the C++ class is blueprinted into a blueprint class, just like how we can override the CDO’s CreateDefaultSubobject in C++, we should be able to override the Default Subobjects in Blueprint… After all, that blueprint class has to be read from disk anyways…

I guess this could be done via the ConstructionScript in blueprint if the UPROPERTY() has BlueprintReadWrite on it.

---

<div class="post-metadata">

**Author:** ![jwatte](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jwatte/32/744941_2.png) [@jwatte](https://forums.unrealengine.com/u/jwatte)\
**Post date:** [November 24, 2021, 1:14am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/4 "2021-11-24T01:14:11Z")

</div>

I agree; it seems like it should be possible to override which class is used in a CreateDefaultSubobject\<\> instantiation. All the data necessary should be there for the engine to do the right thing.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [November 24, 2021, 8:59am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/5 "2021-11-24T08:59:24Z")

</div>

It’s been asked for for a long time, but still isn’t supported. The only way to do this IMO, would be to inject into the “Simple Construction Script” that Blueprint uses to create components, and have a pop-up that lets you choose a derived component from the creation dialog box.

There is no CDO for Blueprints, and the C++ Constructor runs a long time before any Blueprint serialization, so no doubt it becomes a bit of a minefield, which is probably why they haven’t done it. For now, I’ve found the simplest method is to just set a Read/Write variable or call a Setter function that throws an error if called outside of the construction script.

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [November 24, 2021, 8:42pm UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/6 "2021-11-24T20:42:41Z")

</div>

> [@Jambax](#):
>
> call a Setter function that throws an error if called outside of the construction script

this caught my eye. is there some method internally of doing this, or are you just setting a bool to flag it?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [November 25, 2021, 11:38am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/7 "2021-11-25T11:38:23Z")

</div>

You can use the existing bActorIsBeingConstructed to guard against it.

---

<div class="post-metadata">

**Author:** ![MDSRS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MDSRS](https://forums.unrealengine.com/u/MDSRS)\
**Post date:** [December 1, 2021, 9:26pm UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/8 "2021-12-01T21:26:15Z")

</div>

From what I understand the construction script is called when the blueprint is moved/updated as well though no? Wouldn’t this cause all the manually set data overrides to be cleared constantly?

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [December 2, 2021, 8:24am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/9 "2021-12-02T08:24:39Z")

</div>

Only when you move or update it in the editor, and if you’re only setting a value it shouldn’t be a problem.

---

<div class="post-metadata">

**Author:** ![ceriseraine](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ceriseraine](https://forums.unrealengine.com/u/ceriseraine)\
**Post date:** [November 14, 2024, 4:18am UTC](https://forums.unrealengine.com/t/replace-a-c-native-component-with-a-derived-class/262230/10 "2024-11-14T04:18:36Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/d/d8d3535f558578c7998297a136f925081171abfc.png)

The VehicleMovementComponent is a native C++ class. It is possible to modifiy the component class here to use the bp version of the component (which I am not, but selecting it would instantiate the BP version of the component). Is it what you are trying to do?
