# Refactor by moving BP components to C++

**URL:** <https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [June 1, 2021, 9:08am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824 "2021-06-01T09:08:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bgolinvaux](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bgolinvaux](https://forums.unrealengine.com/u/bgolinvaux)\
**Post date:** [June 1, 2021, 9:08am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/1 "2021-06-01T09:08:35Z")

</div>

Hello

I have a BP Actor-derived class (`BP_MyActor`)

It contains several BP components that define various behaviors.

For performance reasons (compute-heavy code), it is now time to move some of these components to C++. Let’s call such a component `BP_MyComp`.

However, since `BP_MyComp` is rather big, I do not want to delete it right away but I want, for some time, to be able to switch between the BP implementation and the in-progress C++ implementation.

Being a C++ programmer, my first thought was :

- 
  1. let’s create an interface `IMyComp` where I declare the `BP_MyComp` public functions.

- 
  1. Make `BP_Comp` implement `IMyComp` and reconnect the implemented interface events to their proper implementation (UE unfortunately does not “understand” this refactoring and tells me the existing function clash with the new interface ones… I have to rename the old functions and move their BP graph into the new interface event implementation graph)

This works OK. I can see my C++ defined interface and implement it.

Then, I wanted to remove the `BP_MyComp` statically defined component from `BP_MyActor` (the one that was added with the big green “+ Add Component” button in the actor BP editor) and turn the component into a variable.

The idea was to have a variable of type `IMyComp` and, at runtime, decide (on a flag or some .ini  
setting) whether to instantiate the `BP_MyComp` or a new `Cpp_MyComp`, then assign the result to the `IMyComp` variable, then eventually dynamically add it by calling `AddComponent` on the actor.

It does not work.

It is simply not possible to declare a BP variable with an interface type. It works for BP interfaces, but not C++ interfaces. Unless I have missed a specific macro keyword.

How would you guys approach such a refactoring where BP code is gradually moved to C++ with A/B testing?

A base class maybe? Am I allowed to reparent `BP_MyComp` to a pure C++ base class and use it as the type of a property ?

I am confused by the way UE interfaces are meant to be used. This looks like such a basic pattern…

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Cd1232](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cd1232](https://forums.unrealengine.com/u/Cd1232)\
**Post date:** [June 1, 2021, 9:59am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/2 "2021-06-01T09:59:57Z")

</div>

I was able to create a variable in a random blueprint that was of the type ReactToTriggerInterface. Is your interface blueprintable?

> **[Interfaces](https://docs.unrealengine.com/4.26/en-US/ProgrammingAndScripting/GameplayArchitecture/Interfaces/)**
>
> Reference to creating and implementing interfaces.

You could have an interface, a C++ class that inherits that interface, then the BP\_MyComp can derive from the C++ class. From there you can make functions that are implementable in either C++ or Blueprints. Like so

```auto
/**A version of React To Trigger that can be implemented in C++ or Blueprint. */
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Trigger Reaction)
bool ReactToTrigger();

```

---

<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:** [June 1, 2021, 7:09pm UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/3 "2021-06-01T19:09:04Z")

</div>

To move things to C++, I use implementation inheritance instead.  
Yeah, I’m not a big fan of implementation inheritance, but it’s the Way Of Life in Unreal Engine.

So, assuming I wanted to port a Component blueprint to C++, I’d create a new ActorComponent or SceneComponent called something like MyComponent in C++. It might even be empty to begin with, but it would inherit from whatever base Unreal component class your BP component is inheriting from.

Then I’d go into the component definition in Blueprint and choose “File → Reparent” to make it inherit from my MyComponent instead of the base component type. This should work like a no-op. Make sure you can compile and run at this point.

Now, you can move things from your BP class into your MyComponent C++ class, one field or function at a time if you want. Declare it in C++, remove it from BP, compile and run to make sure it works, repeat.

At some point, you’ve moved all your blueprint functionality into the C++ class, and the blueprint class is empty. Now you can go to all blueprints that use the blueprint-component-class, and reparent them to inherit directly from your MyComponent instead of the blueprint component class.

Finally, you can remove the blueprint class at this point.

---

<div class="post-metadata">

**Author:** ![bgolinvaux](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bgolinvaux](https://forums.unrealengine.com/u/bgolinvaux)\
**Post date:** [June 2, 2021, 8:58am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/4 "2021-06-02T08:58:33Z")

</div>

Hello,  
Thanks for your reply.  
That was exactly my plan. However, once this is done, I cannot declare, in my parent actor, a variable that is of the interface type.  
I need it to be of that type, not the concrete one, because I want to be able to create a different concrete impl based on a runtime condition.

I can’t make my interface `Blueprintable` , because the UBT complains that my interface should derive from `UObject`.

Here’s the code that works in terms of implementation, but does not allow me to declare a BP variable with that type:

```auto
#pragma once

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "CipCompInterface.generated.h"

UINTERFACE(MinimalAPI)
class UCipCompInterface : public UInterface
{
    GENERATED_BODY()
};

class COMPINTERFACEPOC_API ICipCompInterface
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = Ball)
    void PerformCubeMove(float DeltaTime);
};

```

But adding `UCLASS(Blueprintable)` before `class COMPINTERFACEPOC_API ICipCompInterface` triggers the following error :

_|Error||Class ‘ICipCompInterface’ must inherit UObject or a UObject-derived class_

Using a base class, from which I inherit both my C++ and BP implementation _does_ work, and although less elegant imho, that’s the option I have chosen.

---

<div class="post-metadata">

**Author:** ![bgolinvaux](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bgolinvaux](https://forums.unrealengine.com/u/bgolinvaux)\
**Post date:** [June 2, 2021, 9:00am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/5 "2021-06-02T09:00:17Z")

</div>

Hi jwatte  
Thanks for your excellent suggestion.  
I was afraid that I could not reparent the (big) existing Blueprint component but this works.  
I was hoping that the editor would “understand” that reparenting to a base class that contains declarations for existing events/functions would automagically “rewire” them, but this is not the case. I need to move the graph from the older events to the newly implemented ones, and change the calling sites.  
That’s ok, though.

Thanks again 🙂

---

<div class="post-metadata">

**Author:** ![bgolinvaux](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bgolinvaux](https://forums.unrealengine.com/u/bgolinvaux)\
**Post date:** [June 2, 2021, 3:55pm UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/6 "2021-06-02T15:55:38Z")

</div>

BTW, I forgot to mention that I really want to do A/B testing.

This is why I settled for an alternative version of your suggestion where the base class doesn’t do anything, and I choose between two concrete implementations of this base class upon actor creation (I am doing it in `BeginPlay` but I might want to move it somewhere earlier if this proves dangerous)

The only downside is that I cannot add the component the standard way and need to call `Add XXX Comp` explicitly (+ store it inside a variable for easier use).

---

<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:** [June 4, 2021, 11:27pm UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/7 "2021-06-04T23:27:08Z")

</div>

> [@bgolinvaux](#):
>
> I can’t make my interface `Blueprintable` , because the UBT complains that my interface should derive from `UObject`.

An interface needs to derive from `UInterface`, and it needs to be `BlueprintType`, not `Blueprintable`. `Blueprintable` means it has data members, which interfaces never do.

Your actual interface functions need to be `BlueprintNativeEvent`, and thus follow that convention where your C++ implementation goes in the `_Implementation` function variant.

---

<div class="post-metadata">

**Author:** ![bgolinvaux](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@bgolinvaux](https://forums.unrealengine.com/u/bgolinvaux)\
**Post date:** [June 15, 2021, 8:48am UTC](https://forums.unrealengine.com/t/refactor-by-moving-bp-components-to-c/232824/8 "2021-06-15T08:48:21Z")

</div>

Hello  
Thank you very much. Using `BlueprintType` indeed allowed me to declare variables inside my actors with this type.

However, I wanted to use this for `UActorComponent` subclasses (since my existing BP code is an `UActorComponent`) and I receive the following error :

```auto
C:/wb/sandbox/CompInterfacePoc/Source/CompInterfacePoc/CipCppCompFromInterface.h(12) : Error: Interface class 'UCppCipCompFromInterface' cannot inherit from non-interface class 'ActorComponent'

```

I decided to go with the “common C++ base class” method and, so far, it appears to work nice.

My only issue at the moment is that I can’t have functions that are both `BlueprintNativeEvent` (to implement them in both my BP and C++ components) and that are Server RPC. I must go trough a wrapper function. No big deal but it makes the code a little messier.

Thanks for everyone for their kind help.
