Interfaces - BlueprintNativeEvent cannot be overriden in code?

[SOLVED]

I’ve been trying for some hours to make a simple interface class work, without success.

My goal is to implement a Interface Class for my weapon(WeaponFireInterface) with a single method: virtual void RequestPrimaryFire();

The method should be called by blueprints, and overridable by a C++ class.

I can’t make it work using this setup inside my method:

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = “RequestFire”)
virtual void RequestPrimaryFire();

It seems that BlueprintNativeEvent prevents the method to be virtual.

Error msg :2> F:/UnrealProjects/_SourceControl/Demoroom_1.1 4.12/Source/Demoroom1_1/WeaponFireInterface.h(23) : BlueprintImplementableEvents in Interfaces must not be declared ‘virtual’

So, here’s my code:
WeaponFireInterface
.h


UINTERFACE(Blueprintable)
class DEMOROOM1_1_API UWeaponFireInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class IWeaponFireInterface 
{
	GENERATED_IINTERFACE_BODY()
public:
	
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "RequestFire")
	virtual void RequestPrimaryFire();
}

.cpp


void IWeaponFireInterface::RequestPrimaryFire_Implementation()
{
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, TEXT("Interface method original call"));
}

Weapon class
.h


class DEMOROOM1_1_API AWeapon : public AActor, public IWeaponFireInterface
{
	GENERATED_BODY()
...
	//INTERFACES
	virtual void RequestPrimaryFire();


.cpp


void AWeapon::RequestPrimaryFire()
{
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Blue, TEXT("Interface method overriden"));
}

Need some help to make this overridable in C++ and be called by Blueprints, too.

Thanks in advance

Found the solution =]

Just changed from UINTERFACE(Blueprintable) to UINTERFACE(BlueprintType).

Some other lines to change:

Interface class


UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "RequestFire")
	void RequestPrimaryFire();

Weapon class
.h


	//INTERFACES
	virtual void RequestPrimaryFire_Implementation() override;

and .cpp


void AWeapon::RequestPrimaryFire_Implementation()
{
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Blue, TEXT("Interface method overriden"));
}

Its working fine now thanks to a friend’s suggestion.

As I am currently working in 4.11, this was a great help for me because nearly all of the interface documentation is now outdated. Rama’s wiki, mixed with the corrections here, allow me to define an interface, get a class to use multiple inheritance to use it, and then define default behaviour for how that class handles the interface’s functions, in C++.

But if I then want one subclass to do something different, how do I override that functionality I defined in C++? Within the blueprint of my subclass I can’t seem to override the function, only call it, despite it being a BlueprintNativeEvent

Solution

Found the solution myself through some more annoying trial and error.

With this code you can implement base behaviour in specific C++ classes for the interface functions you define, while allowing the flexibility to override that functionality in BP subclasses.

Your interface declaration within your class header (the .h file) should be:


	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = MyCategory)
		bool myFunction();
	virtual bool myFunction_Implementation() override;

Note that I have defined the original function and made it BlueprintCallable and a BlueprintNativeEvent.

What this line does:



virtual bool myFunction_Implementation() override;

is tell your class, that it has a function of this same name and signature to inherit from the interface, which is how calls to the interface functions are able to interact with this class.

the lines I have added:


	UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = MyCategory)
		bool myFunction();

This tells your class that you can both call and override this function in blueprints.
So you need this part as well if you want to be able to override C++ functionality within BP, as BlueprintNativeEvents are intended.

Within your blueprint, on the bar where your functions and variables are located, you will find your interface and function.
interface.png

To override it, right-click on it and click Implement Function.
Interface2.png

Your function will then move into the Function category with the rest of your blueprint’s functions.

CRITICAL TO NOTE
Your function MUST have a return value. If your function returns void, UE4 treats it as an event, and you cannot override it (it will not appear in this list). Just have it return a garbage value if you don’t need a return value.

PM me if you have any problems, I will aim to update Rama’s Wiki soon

4 Likes

Ah thanks man! Been beating my head since quite some time. I’m glad that we have the internet, such issues would be impossible to resolve for a lot of people.

I wonder how StarfireDev used a function in his interface which returns void and still managed to override the function in his C++ class… ?

I don’t think that using override in the interface function declaration would compile, no? It should rather be used in the class which implements the interface, right?

— — — UPDATE — — —

Never mind, the problem was not with the return type. I could use void return type without any problems. Forgot to add empty body to the interface method declaration. Only pure virtual functions can compile without definition in the declaration class, else you must add a body.