I’m overriding a BlueprintNativeEvent type function’s _Implementation. Everything works fine, but I realized I wasn’t sure which base method should I be calling in order to correctly call base classes implementation of the function, Super::MyFunction() or Super::MyFunction_Implementation()? Or does it perhaps not matter which one is called?
override Super::MyFunction_Implementation() It is guaranteed to work correctly.
I’m not sure if Super::MyFunction() would work correctly since IIRC BlueprintNativeEvent functions cannot be virtual but their _Implementation is virtual.
Even then there’s no point in calling Super::MyFunction() since it’ll lead to call of _Implementation in the end anyway and will add a very small overhead of call stack.
MyFunction_Implementation, I’m fairly sure.
Calling MyFunction would lead to infinite recursion I’d think, since it will internally invoke a virtual call of the implementation function and you’ll come back to where you started.
nope, it’ll not lead to recursion since it’ll call parent versions of function and implementation , that’s the whole point of **Super:: **
Thanx!
I have tried and both seemed to work equally. I wasn’t sure if calling one could lead to unexpected problems in certain cases or something like that.
No, it will call the parent version of MyFunction. That’s a generated function which internally has no idea whether it was called by specifying Super:: or otherwise - it will just invoke the implementation function, which as I said will create a loop.
What if generated method be safe by being specific like
void Foo::Bar()
{
Foo::Bar_Implementation();
}
You can see what the generated method looks like by finding the corresponding .generated.cpp file. In reality it’s more complicated than simply invoking the implementation method, because it first needs to check if there is a blueprint override, and if so execute that instead. So it actually calls the ProcessEvent function.
The point though is that the implementation call will always be virtual. It has to be, otherwise there would be no point in overriding the implementation method in derived classes. If it was set up in the manner you suggest above, no overrides would ever be called.
**** right, I overlooked that aspect, You’re correct.
sooooooo how do you call Super:: without causing infinite loop? no matter what i do its infinite loop crash
You’re meant to do it like this. You can easily enforce the correct behaviour by not making the initial declaration virtual (it doesn’t need to be, only the _Implementation declaration does). So for the following use case:
Class A
.h:
UFUNCTION(BlueprintNativeEvent, Category = "MyFunctions")
void MyFunction();
virtual void MyFunction_Implementation();
---
.cpp:
void MyClass::MyFunction::Implementation()
{}
Class B
.h:
virtual void MyFunction_Implementation() override;
---
.cpp:
void MyClassB::MyFunction_Implementation()
{
Super::MyFunction_Implementation()
}
Note: You need to be using GENERATED_BODY instead of GENERATED_UCLASS_BODY to manually declare the _Implementation.