Important: What is listed below is not recommended. If you want to call blueprint functions from C++, you should almost certainly be using a C++ baseclass or interface, with BlueprintNativeEvent or BlueprintImplementableEvent, instead.
If you need to call a function that’s defined in a blueprint from C++, the following code will do it:
struct
{
double X = 100;
double Y = 10;
bool ReturnValue;
} Params;
UFunction* Func = FindFunction("BpFunction");
ProcessEvent(Func, &Params);
if(Params.ReturnValue)
{
//function returned `true`
}
The parameter to FindFunction
must be the name of the function. It will return nullptr
if the function is not found, so you may want to check for it. FindFunction
and ProcessEvent
exist on any UObject
child, such as actors and components.
The struct is used to pass parameters and get the return value. The types must match the ones used in blueprint, but the names do not. If the function returns something, the last member of the struct should be a property matching it.
If the function doesn’t take parameters or return anything, you can pass in nullptr
. Note that you may encounter a crash if the parameter struct doesn’t match correctly.
(Thanks to Eren on Unreal Source Discord)