How do I create a Custom Event in C++?

Hi :slight_smile:

I want to make a custom event in C++. I already watched the new Tutorial Series.

They use “UFUNCTION(BlueprintNativeEvent)”

So i did that:

UFUNCTION(BlueprintNativeEvent, Category = "DmgSystem")
	void TakeDmg(int32 Damage);

Now my problem:
Error 2 error LNK2005: “protected: void __cdecl AUltimateTutorialsCharacter::TakeDmg(int)” (?TakeDmg@AUltimateTutorialsCharacter@@IEAAXH@Z) already defined in UltimateTutorialsCharacter.cpp.obj E:\GitHub\UE4—UltimateTutorialMap\UltimateTutorials\Intermediate\ProjectFiles\UltimateTutorials.generated.cpp.obj UltimateTutorials

As you read the error function code already is decelered in UHT generated code. Accoring to this:

https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Functions/Specifiers/BlueprintNativeEvent/index.html

If you want to create override for event in C++ you need to create funcfion called TakeDmg_Implementation(int32 dmg) in cpp

If you want to create event only used in blueprint and triggered from C++ uss BlueprintImplementableEvent insted

Thank you very much!
I guess I should check the Wiki first next time :slight_smile:
It’s just confusing because on the official tutorial they don’t do “_Implementation”

Nope doesn’t work.

	UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "DmgSystem")
	void TakeDmg_Implementation(int32 Damage);

I can Override the Event in BP. But when I try to call the function outside of my Character (after casting to my char) it tells me I can only call the function inside my character.
So I thought, let’s not override it in BP, let’s do it in C++.

So I tried to define it in my Source File. Error: Already Defined in UltimateTutorialsCharacter.cpp.obj

So I tried to define in my Header File. Error: function ‘void AUltimateTutorialsCharacter::TakeDmg_Implementation(int32)’ already has a body path\UltimateTutorials.generated.cpp

remove _Implementation if you use BlueprintImplementableEvent, i think you don’t need to do that even BlueprintNativeEvent as _Implementation is generated in UHT generated header file

So do this if you want to use event in C++:

UFUNCTION(BlueprintNaiveEvent, BlueprintCallable, Category = "DmgSystem")
void TakeDmg(int32 Damage);

and then _Implementation in Cpp file or if you gonna use event only in Blueprint:

UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category = "DmgSystem")
void TakeDmg(int32 Damage);

And then do nothing in Cpp, just do calls to that function if you want to trigger the event

I WANT TO DO:

Go in my char, override the event

Go to the level blueprint and let a trigger call the event

I CANNOT DO THAT BECAUSE I CANNOT ACCESS THE OVERWRITTEN FUNCTION OUTSIDE OF THE CHARACTER!

SO I TRY TO CODE THE FUNCTIONALITY IN C++!

ok, I understand NativeEvent this way:

Write functionality in C++ ; if needed, override functionality in BP

I CANNOT WRITE ANYTHING IN C++ BECAUSE IT SAYS IT IS ALREADY DEFINED!

Ok heres how it is done with begin play in engine source code

 UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "BeginPlay"))
	virtual void ReceiveBeginPlay();

	virtual void BeginPlay();

And in cpp:

void AActor::BeginPlay()
{
	ReceiveBeginPlay();
}

Did it just like this (changed to my function names ofc)
Error: Unresolved External Symbol

Well, not even the official tutorials work, I guess I will just reinstall the engine -.-

Thanks for your help, I give C++ up ; forever

What external symbol?

I don’t know. Visual Studio says:

Error 2 error LNK2001: unresolved external symbol “public: virtual void __cdecl AUltimateTutorialsCharacter::TakeDmg_Implementation(int)” (?TakeDmg_Implementation@AUltimateTutorialsCharacter@@UEAAXH@Z) E:\GitHub\UE4—UltimateTutorialMap\UltimateTutorials\Intermediate\ProjectFiles\UltimateTutorialsCharacter.cpp.obj UltimateTutorials

Oh sry, I forgot to say: this only happens with BlueprintNativeEvent.

I got it working with BlueprintImplementableEvent.

Still, I want to know how I do BlueprintNativeEvent properly :slight_smile:

I got it working with BlueprintImplementableEvent. But what I really want is a BlueprintNativeEvent.

I did just as you said for the BlueprintNativeEvent (I copied the code over) and I get following error:

Error 2 error LNK2001: unresolved external symbol “public: virtual void __cdecl AUltimateTutorialsCharacter::TakeDmg_Implementation(int)” (?TakeDmg_Implementation@AUltimateTutorialsCharacter@@UEAAXH@Z) E:\GitHub\UE4—UltimateTutorialMap\UltimateTutorials\Intermediate\ProjectFiles\UltimateTutorialsCharacter.cpp.obj UltimateTutorials

My whole Project is on Github. So if you want to take a look.
https://github.com/CAPTNCAPS/UE4---UltimateTutorialMap/tree/experimental
I just pushed the latest files to Experimental Branch :slight_smile:

Oh my god! You are a magican! It compiles and works like it should! :*

(yes, I am very new to programming)

Just 1 more question.

If I place the event in BP, will it be overwritten, or extended by the code I add?

Thanks for the answer :slight_smile:

Hi! Sorry to hear you’re having trouble with this -

Are you trying to do BlueprintImplementableEvent or BlueprintNativeEvent? The first one will only have behavior written in Blueprints, but the second one can also have C++ behavior.

If you want a BlueprintImplementableEvent, just have these lines in your header file - and then no C++ code in your .cpp file.

UFUNCTION(BlueprintImplementableEvent, Category = "DmgSystem")
void TakeDmg(int32 Damage);

If you want a BlueprintNativeEvent, with some functionality in C++ and some functionality adding on or overriding in Blueprints, have these lines in your header file:

UFUNCTION(BlueprintNativeEvent, Category = "DmgSystem")
void TakeDmg(int32 Damage);

And then these lines in your source file:

void TakeDmg_Implementation(int32 Damage)
{
   // your code here
}

Also, if you were getting errors about not being able to call functions externally, make sure that your TakeDmg function declaration is not below a “protected” or “private” line in the Character’s header file.

BlueprintImplementable and BlueprintNative events are both meant to be called from C++, so what you may want to do for your functionality is have two functions. One is BlueprintCallable, and has a C++ definition that calls your BlueprintImplementable or BlueprintNative events. The other is your actual TakeDmg event. I’ll take a quick look and see if that would do what you’re trying to accomplish. :slight_smile:

If you’d like, attach your Character header and source files here and we can take a look!

Good catch! I needed to add your class name into the example code above. Thanks for bearing with me - let’s try this code instead!

Header file:

public:
	UFUNCTION(BlueprintNativeEvent, Category = "DmgSystem")
	void TakeDmg(int32 Damage);

Source file:

void AUltimateTutorialsCharacter::TakeDmg_Implementation(int32 Damage)
{
// your code here
}

I just gave that code a quick test compile against 4.2 - let me know if it works for you! :slight_smile:

It will be overridden unless you add a call to the parent. You can see an example in these videos here (although this may be skipping ahead a bit in the tutorial from where you are now).

Overriding C++ with Blueprints - Part 1

Overriding C++ with Blueprints - Part 2

It seems that as of 4.7 you do have to add the _Implementation to the .h file. See this question.