(C++ Novice) Been struggling some time to learn Interfaces

Hello,

I have almost null experience in Unreal C++ programming, just in Blueprints. Ive been struggling to implement a Interface Class within C++, even after following some written tutorials over the forums, answerhub, blogs, I feel I didnt understand correctly how to write this.

So, my goal is to make a WeaponFireInterface class, which has a void RequestPrimaryFire() method

Heres what Ive done so far inside the WeaponFireInterface.h

#pragma once

#include "Object.h"
#include "WeaponFireInterface.generated.h"

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

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

Ok, everything ordinary so far.

Inside my Weapon class, I`ve added this:

UCLASS()
class DEMOROOM1_1_API AWeapon : public AActor, public IWeaponFireInterface
{
	GENERATED_BODY()

and

//INTERFACES
	void RequestPrimaryFire();

When hit build, the error message is: F:\UnrealProjects_SourceControl\Demoroom_1.1 4.12\Source\Demoroom1_1\Weapon.h(88): error C2259: ‘AWeapon’: cannot instantiate abstract class

I’m sure I did not understand correctly how Interfaces are implemented in the Engine’s framework.

Another thing it complains is when I add virtual to the method, inside the Interface class.

Thanks in advance.

First, your interface method should be virtual. Second, in your interface .cpp file you should provide a default implementaction.

void IWeaponFireInterface::RequestPrimaryFire_Implementation ()
{

}

The implementation suffix is required since it is a BlueprintNativeEvent and the compiler will create a thunk to call either the blueprint version or the code version.

Next, in your AWeapon class you want to mark RequestPrimaryFire () as override. It is not a requirement but it can prevent a pot of issues lIke this. Finally, once again you must provide a new method definition in your weapon’s .cpp file.

Thanks for the answer. Still facing a new issue:

now it seems to complain about the virtual inside the Interface Class

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

Weapon interface
.h

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

.cpp

void IWeaponFireInterface::RequestPrimaryFire_Implementation()
{
	return;
}

Weapon class:
.h

//INTERFACES
virtual void RequestPrimaryFire() override;

.cpp

void AWeapon::RequestPrimaryFire()
{
	return ;
}

I realized after I posted this that blueprint native events work differently in interfaces than in classes. I’m trying to resolve this for another question as well.

I’ll get back to you when I figure it out.

See my response to a similar question here. Note at the time of this answer it is not an accepted answer and may be incorrect, but it is a valid workaround.

Got it… so sadly it seems I can’t mix them.

Well, the way is to override it in the Blueprint Actor or make everything in C++.

I’ll keep asking to make sure this is really something that is not predicted inside the engine…

Thanks, Shohei!

Sorry I couldn’t be much help. I am a pure C++ guy, so I rarely, if ever, need to put BlueprintNativeEvents in interfaces.

As such, my workaround is such:

  1. Create an interface without the BlueprintNativeEvent specifier. This would change your interface method to:

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

  2. Create a default empty implementation in your interface’s cpp file.

  3. Implement the interface in your actor class (public AActor, public IWeaponFireInterface).

  4. Override the interface method:

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

  5. Create a BlueprintNativeEvent method in your actor:

    UFUNCTION(BlueprintNativeEvent, Category = “RequestFire”)
    void OnRequestPrimaryFire();

  6. Implement RequestPrimaryFire as such:

    void AWeapon::RequestPrimaryFire()
    {
    OnRequestPrimaryFire();
    }

This means you call the RequestPrimaryFire() method, but the implementation is in the OnRequestPrimaryFire() event. Unfortunately this has the downside that you need to recreate the C++ event and method transfer manually instead of having it handled by the interface.

The decision is up to you. I imagine there are a few different workarounds to this problem, some of which are more programmer friendly, others of which are more designer friendly.

Hey, I found a solution thanks to a friend of mine.

To make that instead of using UINTERFACE(Blueprintable) I should use UINTERFACE(BlueprintType).

It’s working now.

So, for the final result, here’s the code:
.h

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

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

.cpp = no need to add the definition

Inside my Weapon class:
.h

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

.cpp

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

Have already tested with a child class, and it`s overriding it perfectly.

Thanks, again, Shohei!

That is mind-blowingly odd. Like, counter-intuitively odd. I’ll keep that in mind for future reference.