Download

C++ Interfaces

For the life of me I cant figure out why my C++ interface isn’t working. I’ve followed Rama’s tutorial (https://wiki.unrealengine.com/Interfaces_in_C%2B%2B), but it just won’t call my actor’s implemented interface.

MyInterfaceTest.h:



#include "CoreMinimal.h"
#include "MyInterfaceTest.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterfaceTest : public UInterface
{
    GENERATED_BODY()
};

/**
 * 
 */
class ASTROLIB_API IMyInterfaceTest
{
    GENERATED_BODY()

    // Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

    UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Astronomy Star Spawn")
        bool SimpleFunc();

};

MyActor.h:


  UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Test")
    bool SimpleFunc();
    virtual bool SimpleFunc_Implementation() override;

MyActor.cpp:



bool AMyActor::SimpleFunc_Implementation()
{
    return true;
}


OtherActor.cpp:




         AActor * SpawnedActor = World->SpawnActor<AMyActor>(MyActorClassToSpawn);

        bool test = false;
        test = IMyInterfaceTest::Execute_SimpleFunc(SpawnedActor);

        if (test)
        {
            UE_LOG(LogTemp, Warning, TEXT("TEST TRUE"));
        }
        else
        {
            UE_LOG(LogTemp, Warning, TEXT("TEST FALSE"));
        }



The above function always prints false. I’ve been trying to go through the relevant generated cpp file, and added some debuging:




bool IMyInterfaceTest::Execute_SimpleFunc(UObject* O)
    {
        check(O != NULL);
        check(O->GetClass()->ImplementsInterface(UMyInterfaceTest::StaticClass()));
        MyInterfaceTest_eventSimpleFunc_Parms Parms;
        UFunction* const Func = O->FindFunction(NAME_UMyInterfaceTest_SimpleFunc);
        if (Func)
        {
            O->ProcessEvent(Func, &Parms);
            UE_LOG(LogTemp, Warning, TEXT("ProcessEvent section"));

        }
        else if (auto I = (IMyInterfaceTest*)(O->GetNativeInterfaceAddress(UMyInterfaceTest::StaticClass())))
        {
            Parms.ReturnValue = I->SimpleFunc_Implementation();
            UE_LOG(LogTemp, Warning, TEXT("SimpleFunc_Implementation Section"));

        }
        return Parms.ReturnValue;
    }

From this, it looks like the ProcessEvent section is executing, but I think it should be the SimpleFunc_Implementation section. I have no idea why the SimpleFunc_Implementation section won’t execute though…

At first glance, I have “GENERATED_IINTERFACE_BODY()” in my interface.

MyInterfaceTest.h:



#include "CoreMinimal.h"
#include "MyInterfaceTest.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UMyInterfaceTest : public UInterface
{
GENERATED_IINTERFACE_BODY()
};

/**
*
*/
class ASTROLIB_API IMyInterfaceTest
{
GENERATED_IINTERFACE_BODY()

// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Astronomy Star Spawn")
bool SimpleFunc();

};

In the UMyInterfaceTest class replace “GENERATED_BODY()” with “GENERATED_UINTERFACE_BODY”.
In the IMyInterfaceTest class replace “GENERATED_BODY()” with “GENERATED_IINTERFACE_BODY”.

I had the same problem on v4.19 when following the official “Interface” guide from Epic (https://docs.unrealengine.com/en-us/…nce/Interfaces) which was intended for 4.14, which is definitely not so old, maybe since 2016.

And actually the link you provided has this, plus an explanation that GENERATED_BODY() should actually work on v4.18. For me, on v4.19, it doesn’t compile.