How to implement a virtual UFUNCTION?

I want to overwrite a function from blueprint.
Then I want to call that function from C++. ( Using Polymorphism)
For this I thought that a virtual function would be the correct way.
However I have errors while compiling.

This is the code in C++:

This is the code in Blueprint:

What is the correct way to do it?

Thank you so much!!

this seem to be the way… i have not compiler errors but i need to test it.

1 Like

Header

		UFUNCTION(Client, Reliable, BlueprintCallable)
		virtual void UpdatePlayerListWindow(const UPlayersProfile* PlayersProfile);	
		
// this is what you override in bp
		UFUNCTION(BlueprintImplementableEvent,BlueprintCallable)
			void UpdatePlayerListWindowBP(const UPlayersProfile* PlayersProfile);

cpp


void AaicppGameMode::UpdatePlayerListWindow_Implementation(const UPlayersProfile* PlayersProfile) {
	if(GEngine)
		GEngine->AddOnScreenDebugMessage(1, 1, FColor::Red, "UPDATE PLAYER CLIST WINDOW C++");

	UpdatePlayerListWindowBP(PlayersProfile);
}


If you want the event UpdatePlayerListWindowBP to turn into a function then it needs a return value.

1 Like

You have various options:

  1. BlueprintNativeEvent, this is a function that has a base implementation on C++ and can be overridden in blueprints, in C++ you define the “_Implementation” one, you have to call the one that doesn’t end in “_Implementation” in C++
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
void RemoveNegativeAlteredStates();
void RemoveNegativeAlteredStates_Implementation();

If it’s overridden in blueprints and you want to call the base functionality as well, you have to “add call to parent function” by right clicking the function node, if you’re not going to override it in child C++ classes, you can omit the “_Implementation” declaration, you do have to define it though.

If you have to override the base C++ functionality in C++ child classes, you have to add the virtual modifier to the “_Implementation” declaration, like this:

UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
void RemoveNegativeAlteredStates();
virtual void RemoveNegativeAlteredStates_Implementation();

You can’t omit the “_Implementation” declaration in this case.

  1. BlueprintImplementableEvent, same as above, but it doesn’t have a base implementation in C++:
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
void RemoveNegativeAlteredStates();

You can do other stuff like what the previous poster suggested

A pretty good video I found on the topic is this one: UE4 - Blueprints to C++ Episode 3 - UFUNCTION - YouTube

I also recommend you sift through the specifiers for the different macros, benui has a pretty good compendium and documentation (at least, it’s more complete than the official one), I haven’t found that info on the community wiki: Unreal Engine Documentation · ben🌱ui

I think there are some caveats when using the RPC specifiers though, if you only need local calling of the function (like in the example you provided), I would go for the blueprint implementable event

3 Likes

It’s exactly what I needed
Thank you very much for your help

Thank you very much for such a detailed explanation. It will be very useful for me. Very appreciated.