Call A BluePrint function from C++

I am using C++ and I have a BluePrint function library callled MyFunction which displays a message to the output log.

How can I call this function from C++?

I am at a complete loss here :confused:

One way would be to create an BlueprintableEvent allowing a blueprint to be triggered and call the function.

I didn’t have success when I tried this, due to using a Mac and how XCode has to be started in a certain manner to make it work (I wasn’t aware of that at the time I attempted the event generation) but I think it’s the correct way to do it. Instead I ended up using communication via variables between blueprints and C++. For example, creating a bool visible to the extended blueprint that is interrogated in the blueprint and thus lets you do anything a blueprint can do (in your case, make the function call). Either way, your extended blueprint from your C++ class should be able to be informed to call the library functions. I can’t help concerning the library call, because I don’t use any blueprint libraries.

Using my method, if the function to be called requires a variable to be passed from C++, you will have to also make that variable visible to the C++ Class’s blueprint, then pass it to the library function. Using the event method (the better approach), note how Rama shows passing the value of variable “CurrentHealth” in the linked example. If CurrentHealth were needed to then pass to the library function, it would be available for that purpose.

While these are useful techniques, hopefully someone else will answer if there is a way to directly call the blueprint library function. I’m not aware of any.

Note, if you use a Mac, to do any of this requires starting the editor from XCode or C++ so variables and events will be seen in the blueprint. This is because the hot reload doesn’t make the blueprint aware of new variables or events unless previously built in XCode prior to starting the Editor. I don’t know the details on windows, but I presume it probably “just works”. I just mention this because I wasted weeks trying to get blueprint<->C++ communication working due to this still unresolved bug.

On Windows it Just Works ™

If you had a blueprint function called SetBlockNumber and it takes an integer.
Then in C++ you can use the UObject::CallFunctionByNameWithArguments function to call it like this.

const FString command = FString::Printf(TEXT("SetBlockNumber %d"), block.BlockId);
FOutputDeviceDebug debug;
blockActor->CallFunctionByNameWithArguments(*command, debug, this, true);