Tutorial: BlueprintNativeEvents

A short tutorial describing how to effectively use BlueprintNativeEvent UFUNCTION specifier in C++ and Blueprint

Updated about 1 year ago Edit Page Revisions

Overview

BlueprintNativeEvent is a UFUNCTION specifier which can be added to functions in C++. BlueprintNativeEvents allow programmers to define functions in C++ and override them in Blueprints.

BlueprintNativeEvents are distinct from BlueprintImplementableEvents because a BlueprintNativeEvent can have a C++ implementation!

Here's an example usage from Unreal Engine's GameMode class:

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Game")  
APawn* SpawnDefaultPawnFor(AController* NewPlayer, class AActor* StartSpot);

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="Game")
class AActor* FindPlayerStart( AController* Player, const FString& IncomingName = TEXT("") );

BlueprintNativeEvents shine in situations like this where a C++ implementation is needed, but a Blueprint may want to override the functionality and replace it with their own.

BlueprintNativeEvent in C++

///Header (.h) function definition: 
//Override in BP to extend the base C++ functionality!
UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category="JoyBall")
float GetArmorRating() const;

/////////////////

///Source (.cpp) function Implementation:
float AJoyBall::GetArmorRating_Implementation() const
{
    //remember to call super / parent function in BP!
    V_LOG("C++ Happens First");
    return 100;
}

Please note that you do not have to declare the function signature GetArmorRating_Implementation() in the .h file!

Naming BlueprintNativeEvents in CPP
Standard C++ conventions require that every function declared in a Class.h file has a corresponding function definition in a Class.cpp file.

However, because of Unreal Engine's reflection system, any BlueprintNativeEvent will automatically have its corresponding definition created. For the example above, the function float AJoyBall::GetArmorRating() const is automatically generated.

In order to define your own C++ definition of a BlueprintNativeEvent function, you will have to create a definition by appending _Implementation to the function name. The function definition otherwise remains the same. This can be seen in the example above, with the definition of float AJoyBall::GetArmorRating_Implementation() const

BlueprintNativeEvent in Blueprint

If you have a class with a BlueprintNativeEvent and you create a Blueprint of that class (such as GameMode) then you will see all functions declared as BlueprintNativeEvent will appear in the Function -> Override drop-down of that Blueprint.

File:NativeEventImplinBP.jpg

Click on Override to see a list of all overrideable BP Native events. You can select one of these functions and it will add a node to the EventGraph of your Blueprint!

File:BPNative2.jpg

Using BlueprintNativeEvents in Blueprint

There are two primary ways to use BlueprintNativeEvents in your Blueprints.

  1. To Extend native C++ Behavior
  2. To Override native C++ Behavior

Extending Behavior with BlueprintNative Events

If your goal is to extend rather than replace native C++ behavior for your function, then you will need to ensure that the EventGraph of your Blueprint adds a call to the parent function.

In the screenshot above, the function GetArmorRating is overridden in Blueprint. However, the Blueprint calls Parent: Get Armor Rating which ensures that the native C++ functionality is executed.

Adding Call To Parent Function

In order to add a call to the Parent of a function, right click on the function node and choose "Add Call To Parent Function."

Override Behavior with BlueprintNative Events

If you want to completely override the C++ defined behavior of a BlueprintNativeEvent function, then you can Override the function and purposefully not call the Parent function.

If you do this, the only logic that is performed when the function executes for your Blueprint class will be the nodes implemented in your EventGraph.

Common Errors

Overloaded Function not found...

If you are getting a strange compile error involving overloaded functions and you know the signatures match between .h and your _Implementation, then it means UnrealEngine generated a function parameter list for the _Implementation that does not match what you supplied in the .h

For example:

void SetMenu(FString Title)

will be generated as

void SetMenu(const FString& Title)

and thus you will get a compile error until you change your .h to be:

void SetMenu(const FString& Title)

Conclusion

Using BP Native Events you can offer your teammates powerful C++ tools that can be directly extended in blueprints!