For the life of me I cant figure out why my C++ interface isn’t working. I’ve followed Rama’s tutorial (A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums), 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…