How to add C++ overridable logic to Blueprint ?

Hello,

I’m working on a half Blueprint / half C++ project where I’m trying to add some some C++ logic to an already existing Blueprint which is inherited by other Blueprints.

I’d like to be able to call the C++ function from blueprint scripting and to be able to specialize the behaviour of my C++ function for the children Blueprint.

I’ve reached these conclusions and i might’ve misunderstood something, could you please review/correct it and maybe offer other solutions from your experience ? Thanks.

  1. The C++ interface solution isn’t working since i cannot specialize the code for the children and can’t call C++ defined method from blueprint scripting since BlueprintNativeEvent will override the C++ code as blueprint script
  2. Having a C++ class as parent for my Blueprint won’t let me override the C++ method in children blueprint which will either have to inherit from C++ specialized class (to have specialized functions) or Blueprint class (to have Blueprint Scripting logic, variables etc…) and can’t inherit both
  3. The only “workaround” i found is to make the Blueprint own my C++ class as an object variable, which then allow my Blueprint children to have a specialized C++ class which can have its own logic and remains callable.

For that either use BlueprintImpmentableEvent or BlueprintNativeEvent, they will show up as event and by making event you technically overriding and in case of native event it will even block off parent C++ code or else you call it in blueprint. If event have return it will show in override button in function list.

Native event may introduce some annoyance in C++ child classes as you will need to use _Implementable functions, but one practice that engine it self using to avoid that is make separate implementable event that you call on event function in C++ that is not specfiied as blueprint event and use DisplayName meta tag to make it look like actual function, this is how BeginPlay or Tick is working. In this case in C++, parent call (from Super) becomes point whew blueprint code is applied, that said this does not block off C++ code (you definitely expiranced that with BeginPlay or Tick).

Note that there not much custom magic in blueprints, engine implements many things same way as you do in game project (on C++ level there practically no difference between engine and game code and also plugins, you making module that is just added to set of already exiting once in engine and you code becomes engine code… thats why it crashes engine and editor when you make errors), so you can look up what how engine it self deals with it and look up source code.

Hello, as i said these won’t work in.
To give an example, i have a blueprint class named Parent and a Child one, i need to add some optimization on a blueprint script and make a function in c++, that needs to be redefined for the Child class.
Neither the two keywords achieve what i want because

  1. BlueprintImplementableEvent won’t let me define it in c++
  2. BlueprintNativeEvent won’t let me call a function (with a return value) defined in c++ from blueprint.

And if somehow i could do it with BlueprintNativeEvent (please explain how would that work if you found a way to do it), then I can’t redefine it for my Child class.

First of all thanks for your anwer.

About your first paragraph sadly i cannot redefine these later in c++ and that is a problem.
You explain some good ways to use blueprint from c++, but i’m looking for the opposite (call c++ functions from a Blueprint class)

Let’s say i have a Blueprint Class A which is the parent of Blueprint Class B.
I need to implement C++ function to class A and specialize that function for the class B.
How can i do that ?

Hello! Take a look at things like BlueprintNativeEvent and BlueprintNativeEvent https://answers.unrealengine.com/questions/389350/view.html

The interaction between Blueprint and C++ regarding is not super great when it comes to virtual functions. In C++ you can call any accessible version of a virtual function from anywhere in the inheritance chain by just specifying the appropriate class namespace with the function call. You generally can’t do that in Blueprint but there are workarounds. First step is to mark your C++ function “BlueprintCallable” and “BlueprintNativeEvent”. Let’s call this function “func” and the C++ class “C” and have a Blueprint class “B” derive from it. You already know that you can override “func” in Blueprint via the dropdown menu. You can also add a call to the C++ parent implementation of “C” by right-clicking the function node and selecting “Add call to parent function”. What most people don’t know is that you can copy that call-parent-node and paste it anywhere in your Blueprint to call the parent function. AFAIK there’s no other way to get that node to show up in Blueprint which very weird but whatever. To be clear: you don’t have to actually override the function in Blueprint, you just have to press override to get to the call-parent-node, but then you can copy it out and delete the overriden function again. Let’s assume you have another Blueprint “B2” that inherits from “B”. If you have not overriden “func” in “B” you can call the C++ implementation in the same way, but if you did override “func” then you can’t get to the C++ implementation directly anymore from “B2” (to my knowledge). But you can work around this by implementing a Blueprint function in “B” that just calls the C++ parent function with the trick I previously mentioned e.g. “funcCpp”. So then, if you have overridden “func” in “B” and “B2”, you can call all three version of “func” from “B2”: Call “func” directly in “B2” for the “B2”-implementation, call the “B” implementation with the “call to parent function” node, and call the “C” C++ implementation by calling “funcCpp”.