# C++ Interface Cast returns NULL when implemented with Editor

**URL:** <https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784>\
**Category:** UI\
**Tags:** interface, unreal-engine, Code, null, Blueprint, question, CPP\
**Created:** [October 9, 2015, 4:54pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784 "2015-10-09T16:54:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ghar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ghar](https://forums.unrealengine.com/u/Ghar)\
**Post date:** [October 9, 2015, 4:54pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/1 "2015-10-09T16:54:34Z")

</div>

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

![62062-interfacecoversarea.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/0/2/802926ad91ab51632f1c27a53e08a3a337aec018.jpeg)

I get NULL from

```
Cast<IInterfaceCoversArea>(OwnerActor);

```

What could be the problem?

---

<div class="post-metadata">

**Author:** ![Ghar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ghar](https://forums.unrealengine.com/u/Ghar)\
**Post date:** [October 9, 2015, 10:34pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/2 "2015-10-09T22:34:55Z")

</div>

Omg, figured it out. I found the solution [\>\>here\<\<](https://wiki.unrealengine.com/Interfaces_And_Blueprints).

I needed to use the following syntax, it works on both cases: when implementing the interface in code and purely on blueprints

```
if (TargetActor->GetClass()->ImplementsInterface(UInterfaceCoversArea::StaticClass()))
{
	CurrentRange -= IInterfaceCoversArea::Execute_GetAreaCoverageRadius(TargetActor);
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_4b3639a6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_4b3639a6](https://forums.unrealengine.com/u/anonymous_user_4b3639a6)\
**Post date:** [June 14, 2016, 6:17pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/3 "2016-06-14T18:17:47Z")

</div>

Hey Ghar 🙂

I wrote a tutorial about Interfaces which can be implemented by Blueprints and C++ Classes.  
Maybe you will find a more elegant solution in my attempt 🙂

[How To Make C++ Interfaces Implementable By Blueprints(Tutorial)](https://wiki.unrealengine.com/How_To_Make_C%2B%2B_Interfaces_Implementable_By_Blueprints(Tutorial))

Good luck

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_acbc7c02](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_acbc7c02](https://forums.unrealengine.com/u/anonymous_user_acbc7c02)\
**Post date:** [October 27, 2016, 4:50am UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/4 "2016-10-27T04:50:56Z")

</div>

Same issue.

The cast to the interface looks like there is an engine bug. Sad to have to use ghar’s workaround - especially given that Rama’s tutorial with the Cast is one of the first google hits.

UObjectBaseUtility::GetInterfaceAddress is what is doing the casting. The c++ interface has the CLASS\_Native flag (because it was written in C++), so it goes to the native lookup. This part looks like a copy paste of ::GetNativeInterfaceAddress - which fails if bImplementedByK2 is true (which it is in this situation).

removing that flag check allows it to properly return the interface structure (with 0 offset, so the calculation is fine), but of course this is an engine change.

Hopefully staff will weigh in?

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [October 15, 2017, 5:00am UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/5 "2017-10-15T05:00:32Z")

</div>

This won’t work. If it’s implemented in BP, then at the C++ level the object does not inherit from the C++ interface and so it doesn’t have the virtual function table entries for the methods. Allowing the cast to return the address is just a recipe for crashing the engine.

This is simply not a bug, it’s a limitation of the way BP is built on top of C++. The accepted answer is not a workaround, it’s the (admittedly ugly) intended approach.

---

<div class="post-metadata">

**Author:** ![Angel\_Phosphoros](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Angel\_Phosphoros](https://forums.unrealengine.com/u/Angel_Phosphoros)\
**Post date:** [March 7, 2019, 1:01pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/6 "2019-03-07T13:01:17Z")

</div>

I wrote very clear way to do this.

Snippet from here: [Interface implementation check for Unreal Engine 4 (UE4) ($1832992) · Snippets · District Zero / Unreal Engine 4 Snippets · GitLab](https://gitlab.com/district0/unreal-engine-4-snippets/snippets/1832992)

Code:

```
#pragma once

#include "Interface.h"

/**
* Fast and full checking of interface implementation including nullptr check, C++ cast check and blueprint check
* @param TIInterfaceType is interface IClass (not UClass)
* @param object is pointer to check
* @return true if object implements TIInterfaceType, false if nullptr or doesn't implement
*/
template<typename TIInterfaceType>
FORCEINLINE bool IsInterfaceImplementedBy(const UObject* const object)
{
    return 
        // Nullptr is invalid
        object && 
        // C++-style cast much faster
        (Cast<TIInterfaceType>(object) || 
        // But blueprint implementation pass only this check
        object->GetClass()->ImplementsInterface(TIInterfaceType::UClassType::StaticClass()));
}

```

You need to include this header into every interface declaration and use it in C++:

```
void ATestCodeGameModeBase::CallDoSomething(UObject* object) 
{
    // Clear check of what we mean
    if (IsInterfaceImplementedBy<ITestInterface>(object))
    {
        ITestInterface::Execute_DoSomething(object);
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Object does not implement interface"));
    }
}

// vs

void ATestCodeGameModeBase::CallDoSomething(UObject* object)
{
    // Doesn't understandable
    if (object && object->GetClass()->ImplementsInterface(UTestInterface::StaticClass()))
    {
        ITestInterface::Execute_DoSomething(object);
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Object does not implement interface"));
    }
}
```

---

<div class="post-metadata">

**Author:** ![Angel\_Phosphoros](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Angel\_Phosphoros](https://forums.unrealengine.com/u/Angel_Phosphoros)\
**Post date:** [March 7, 2019, 1:02pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/7 "2019-03-07T13:02:57Z")

</div>

I wrote clear approach to check interface types. See my answer.

---

<div class="post-metadata">

**Author:** ![MaxPower42](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MaxPower42](https://forums.unrealengine.com/u/MaxPower42)\
**Post date:** [September 26, 2019, 9:32pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/8 "2019-09-26T21:32:41Z")

</div>

So, does this mean there is no way at all to call a native interface function on a blueprint-implemented interface (but only what’s exposed to BP)?

---

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [May 22, 2022, 11:26am UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/9 "2022-05-22T11:26:22Z")

</div>

> [@Angel\_Phosphoros](#):
>
> ```auto
> if (object && object->GetClass()->ImplementsInterface(UTestInterface::StaticClass()))
> {
> ITestInterface::Execute_DoSomething(object);
> }
> 
> ```

Is this still working?  
For me this Execute function always returns NULL result.

---

<div class="post-metadata">

**Author:** ![FrancoACT](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancoACT](https://forums.unrealengine.com/u/FrancoACT)\
**Post date:** [December 22, 2023, 1:22pm UTC](https://forums.unrealengine.com/t/c-interface-cast-returns-null-when-implemented-with-editor/334784/10 "2023-12-22T13:22:53Z")

</div>

Hi! I had the same problem, I created an interface on C++ and added in the editor to a Widget Blueprint, but the cast return a null.

```auto
IResultsInterface* sr = Cast<IResultsInterface>(object);
if (sr) sr->SetSumaryData(data); //fail

```

The solution was to use the static version of the function:

```auto
if (object->GetClass()->ImplementsInterface(IResultsInterface::StaticClass()))
{
	IResultsInterface::Execute_SetSumaryData(b, data);
}

```

Engine was 5.2.
