Create c++ class for existing blueprint

Hello,

I have created a pure blueprint actor and added some logic in blueprint there. However now I realized that would be better of implementing this logic in c++. How can I create a c++ class for an existing blueprint? I tried to create the class as a new c++ actor with the same name, but it complains that the class with this name already exists.

Hello, Oleg

Please note, that when a game is being developed in Unreal Engine 4, Blueprints are mostly used by designers. This means, that at the moment of Blueprint creation, necessary classes have already been developed by programmers. With this, both groups can effectively focus on their tasks.

Thus, the required functionality isn’t possible. Due to engine designers’ intent, you should actually do the opposite and create blueprint after implementing required functionality in C++ class, since it will be more productive.

Hope this helped!

3 Likes

Thank you for your answer! I’m a novice and I somehow missed this advice in the documentation. At this point I don’t want to loose created blueprint code and my level where I placed dozen of objects of this class. What is the best strategy to transfer what I have to a recommended C++ and Blueprint pair of classes? E.g. I can create different C++ class with attached blueprint, copypaste bluprint code to it. Can I mass-replace object of certain class to another class? Is this a recommended way to do it?

After reading some docs I found a workaround.
In my case I had a lot of boxes with Cube static mesh I placed in the level so I wanted to preserve their placement. Also I wanted to preserve the blueprint code I wrote. Lets say the name of the blueprint-only class is “BPOnly”.

  • Create a new C++ actor and Blueprint for it (e.g. following this tutorial). In my case I create an actor and then added static mesh component “Cube” in the edit. Lets call this class “CPPAndBP”.
  • Remove all event mocks in the new blueprint of “CPPAndBP” such as Begin play and tick
  • Copy-paste blueprint code from your old blueprint “BPOnly” to the new one “CPPAndBP” (select all with Ctrl+A, Ctrl+C then Ctrl+V)
  • If you have variables in the “BPOnly” blueprint, the new one wont compile. So you need to recreate variables in the “CPPAndBP” blueprint. I found the fastest is to select the pin with variable, drag it to empty place and then select “Promote to Variable”
  • Now go to your main level tab. Select the “CPPAndBP”.
  • In the level editor select the instances of “BPOnly” that you want to replace. You can quickly select multiple objects in the World Outliner (e.g. select objects in the folder or via menu “Select”-> “Select All BPOnly”)
  • Make sure that the new class “CPPAndBP” is still selected in the Content Browser. Right click on the particular instance of the old object in the Editor (not in the Outliner).
  • There should be a menu item Replace Selected Actors with “CPPAndBP”. Click it, it should do the trick.

In my case I preserved all the positions and sizes of Cube static meshes through the process and associated Blueprint. But now I can augment instances with cpp logic!

In order to not get to this situation based on 's answer I never create pure Blueprint classes anymore but always start with C++. So far I don’t see a downside of this decision (except maybe longer compilation time).

Accidentally I found exactly what I wanted that works beautifully without any workarounds:

  • Create a C++ class you want to be a parent for the existing blueprint
  • Open a pure blueprint you have
  • Click File->Reparent Blueprint and choose the C++ class you created
6 Likes

Hi ,
It appeared to be a way to do exactly what I wanted - see my answer with blueprint reparenting