Call Blueprint functions from C++

Hello,
How can i call a Blueprint function from C++.
So i have a buttonPressed() function that i call in c++
and now i want to do something when the
buttonPressed() function was called in my Blueprint.

thanks for any answers

PS.: If anyone knows how i can play matinee in C++ it would be also helpful

Hey theJulman-

I’m a little unclear on your question. Is the buttonPressed() function created in the blueprint or in c++? It may be be easier to create the function in code with default functionality, then use blueprints for extended functionality as needed

Cheers

Doug Wilson

//if you want to define a function in CPP that can be redefined or called in blueprint, do this:

//for a function named XXX inheriting from a class named YYY, you would write:

//inside the YYY.h class:

 UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "ZZZ")
 void XXX();

//at the end of the YYY.CPP file:

 void YYY::XXX_Implementation()
 {
   //add code here
   AAA.doStuff();
 } 

//the “_Implementation” suffix for the function name is only needed if you use “BlueprintNativeEvent”, and if you want to call that function from CPP, you just call " XXX(); " and not " XXX_Implementation(); "

So thats how my PlayerCharacter.cpp looks like:

if(HitActor){
    AmyButton* myButton = Cast<class AmyButton>(HitActor);
    if(myButton){
       if(HitActor->ActorHasTag(FName("Play"))){
          myButton->buttonPlay();
       }
       else if(HitActor->ActorHasTag(FName("Reverse"))){
          myButton->buttonReverse
       }
    }
}

And myButton.h:

UFUNCTION(BlueprintCallable, Category = Buttonfunc)
void buttonPlay();

UFUNCTION(BlueprintCallable, Category = Buttonfunc);
void buttonReverse();

The Problem is I don’t know what i should use instead of BlueprintCallable

And here is my LevelBlueprint:

Oke thanks this answer worked for me :slight_smile:

How do you make this?

I ahve no idea where you can plug this in the blueprints, I wasn’t able to find the new UFUNCTION anywhere

Adding UFUNCTION(BlueprintCallable) to the function declaration will expose the function to blueprints. Once you add it to the code and compile again you can open up a blueprint with access to the class and type the name of the function into the right-click search menu. Additionally adding “Category = ABC” after BlueprintCallable will allow you to search for the ABC category and anything (function or variable) with the same category will appear.

Addendum:
If you want to pass parmeters you just have to treat it as a command entry string.

So if i add an integer paramter to the Call Function Test then this works…

c->CallFunctionByNameWithArguments(TEXT("CallFunctionTest 42"), ar, NULL, true);

Ok, got this working so here is an example…

I want to call a blueprint function from C++.

So first I make a blueprint function called CallFunctionTest.
(In our game, and we call a player a “Survivor”.)
It just prints “Survivor Call Function Test” in pink on the console.

Then I have a static blueprint set of functions like this…

The header file is

#include "Kismet/BlueprintFunctionLibrary.h"

#include "TDLSurvivorFastCache.generated.h"

/**
 * 
 */
UCLASS()
class TDL_API UTDLSurvivorFastCache : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()


public:

	UFUNCTION(BlueprintCallable, Category = TDLSurvivorCache)
		static void TestCPPCallToBP(AActor * c);
	
};

and the cpp file is…

#include "tdl.h"
#include "ConsoleManager.h"

#include "TDLSurvivorFastCache.h"
   
UTDLSurvivorFastCache::UTDLSurvivorFastCache(const class FObjectInitializer & PCIP)
	: Super(PCIP)
{
	UE_LOG(TDLLog, Log, TEXT("UTDLSurvivorFastCache"));
}

void UTDLSurvivorFastCache::TestCPPCallToBP(AActor * c)
{
	FOutputDeviceNull ar;
	c->CallFunctionByNameWithArguments(TEXT("CallFunctionTest"), ar, NULL, true);
}

Then we call the test function from our SurvivorBP blueprint and it calls the function on the SurvivorBP.


Here are the blueprints

45483-testfunctionimpl.png

45484-calltotestcppfunc.png

1 Like

Another bit of useful code…

char funcCallBuf[1024];

void UTDLSurvivorFastCache::CallActorFunc(AActor * c, FString funcName)
{
	FOutputDeviceNull ar;
	c->CallFunctionByNameWithArguments(*funcName, ar, NULL, true);
}

void UTDLSurvivorFastCache::CallActorFuncIntParam(AActor * c, FString funcName, int32 f)
{
	_snprintf(funcCallBuf, sizeof(funcCallBuf), "%s %d", *funcName, f);

	FOutputDeviceNull ar;
	c->CallFunctionByNameWithArguments(ANSI_TO_TCHAR(funcCallBuf), ar, NULL, true);
}

void UTDLSurvivorFastCache::CallActorFuncFloatParam(AActor * c, FString funcName, float f)
{
	_snprintf(funcCallBuf, sizeof(funcCallBuf), "%s %f", *funcName, f);

	FOutputDeviceNull ar;
	c->CallFunctionByNameWithArguments(ANSI_TO_TCHAR(funcCallBuf), ar, NULL, true);
}

void UTDLSurvivorFastCache::CallActorFuncFloatIntParam(AActor * c, FString funcName, float f, int32 n)
{
	_snprintf(funcCallBuf, sizeof(funcCallBuf), "%s %f %d", *funcName, f, n);

	FOutputDeviceNull ar;
	c->CallFunctionByNameWithArguments(ANSI_TO_TCHAR(funcCallBuf), ar, NULL, true);
}

Is there a way to do this for a AActor parameter?

This is a better solution to the problem of a callable blueprint function. My above answer is also useful but in a slightly different scenario. I suggest trying this solution first, and my solution second.

Do not Play matinee By C++,if not packet project will fail!

In case you google this and actually want to do what the OP states in the title, here is some code:

To Call Blueprint function node from C++ :

1 - Expose a function handle in C++ Header:

UFUNCTION(BlueprintImplementableEvent, Category = "AI") 
void StartPartrolling();

2 - Add the function node to the blueprint event graph

In this example right click on event graph and search for “event start patrolling” and connect it up to whatever you wan to run

260784-capture.jpg

3 - Call the function StartPartrolling() from your C++ Code

4 - Profit

1 Like

This should be on the MAIN CPP DOC PAGE !!!

I haven’t tested it, but I assume If the function name you pass into CallFunctionByNameWithArguments() is misspelled or doesn’t exist the compiler (and IDE) won’t catch it, and you’ll get a run-time error / crash? I’d prefer to use a technique that would be picked up before that (for example the technique I posted below) by the complier or IDE. IMHO leveraging the power of the IDE will increase your development speed, accuracy and quality.

That’s clearly the easiest and cleanest way to do it. Thanks for that!

UFUnction* Func = Obj->GetClass()->FindFunction(FName(“FuncName”));
if(Func == nullptr){return;}

FStructOnScope FuncParam(Func);
UProperty* ReturnProp = nullptr;

for (TFieldIterator<UProperty> It(Func); It; ++It)
   {
	UProperty* Prop = *It;
	if (Prop->HasAnyPropertyFlags(CPF_ReturnParm))
	{
		ReturnProp = Prop;
	}
	else
	{
		//FillParam here			
        }
}

Obj->ProcessEvent(Func, FuncParam.GetStructMemory());

I noticed that in some cases, the UFUNCTION is called on the next tick. This causes problems for state machines. Do you know how to ensure a UFUNCTION is called in the same tick as the caller?