C++ Interface Cast returns NULL when implemented with Editor

Ghar
Fri, 09 Oct 2015 16:54:34 +0000
UI

Hello. I made an Interface that should be able to be implemented in both code and Blueprints.

This is the Interface:

InterfaceCoversArea.h

UINTERFACE()
class UInterfaceCoversArea : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class IInterfaceCoversArea
{
	GENERATED_IINTERFACE_BODY()

public:

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, meta = (DisplayName = "Get Area Coverage Radius"), Category = "Interface Covers Area")
	float GetAreaCoverageRadius();
};

My code class implementing it:

ParentCharacter.h

UCLASS()
class AParentCharacter : public ACharacter, public IInterfaceCoversArea
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "ParentCharacter")
	float AreaCoverage = 40.f;
...

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, meta = (DisplayName = "Get Area Coverage Radius"), Category = "Interface Covers Area")
	float GetAreaCoverageRadius();
};

ParentCharacter.m

float AParentCharacter::GetAreaCoverageRadius_Implementation()
{
	return AreaCoverage;
}

This is how I’m using it in another class:

float CurrentRange = OwnerActor->GetHorizontalDistanceTo(TargetActor);
IInterfaceCoversArea* InterfaceCoversAreaActor = Cast<IInterfaceCoversArea>(OwnerActor);
if (InterfaceCoversAreaActor != nullptr)
{
     CurrentRange -= InterfaceCoversAreaActor->Execute_GetAreaCoverageRadius(Cast<UObject>(InterfaceCoversAreaActor));
}

This works with Blueprints that inherti from ParentCharacter, but when I implement the interface using the Editor

I get NULL from

Cast<IInterfaceCoversArea>(OwnerActor);

What could be the problem?

10 posts - 8 participants