# Returning an interface from C++?

**URL:** <https://forums.unrealengine.com/t/returning-an-interface-from-c/332605>\
**Category:** UI\
**Tags:** interface, error, compiling, unreal-engine, Blueprint, CPP, question\
**Created:** [September 18, 2015, 4:24am UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605 "2015-09-18T04:24:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelWion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MichaelWion](https://forums.unrealengine.com/u/MichaelWion)\
**Post date:** [September 18, 2015, 4:24am UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605/1 "2015-09-18T04:24:45Z")

</div>

In my current project, I’m converting some blueprints into C++.  
One of these blueprints is able to search through all actors within the current scene, and return the interface for the first valid object which utilizes said interface:

[https://forums.unrealengine.com/attachment.php?attachmentid=57682&d=1442544455](https://forums.unrealengine.com/attachment.php?attachmentid=57682&d=1442544455)

As you can see, returning an interface works just fine in blueprints. In C++, however, I’ve tried several different ways to return an interface, and can’t get any of them to compile.  
Here’s some of the following setups (and their corresponding errors) that I’ve tried:

* * *

- `IMarionettistInterface *FindMarionettistInScene(TEnumAsByte MarionettistName, bool &Error);`

- `UMarionettistInterface *FindMarionettistInScene(TEnumAsByte MarionettistName, bool &Error);`

- `void FindMarionettistInScene(TEnumAsByte MarionettistName, bool &Error, IMarionettistInterface *Interface);`

- `void FindMarionettistInScene(TEnumAsByte MarionettistName, bool &Error, UMarionettistInterface *Interface);`

- `void FindMarionettistInScene(TEnumAsByte MarionettistName, bool &Error, const IMarionettistInterface &Interface);`

* * *

… Anyhow, I’m sure you get the idea. I’m a bit stumped how this is actually done.

Oh, and in case you’re interested, line #8 of `MarionettistUtilities.h` is `GENERATED_UCLASS_BODY()`, and line #12 is the above function I’m trying to compile (currently, it’s the only one in the class).

**So what is the correct way to convert this blueprint into C++**?

---

<div class="post-metadata">

**Author:** ![Mateusz.Wiech](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mateusz.Wiech](https://forums.unrealengine.com/u/Mateusz.Wiech)\
**Post date:** [September 18, 2015, 1:13pm UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605/2 "2015-09-18T13:13:20Z")

</div>

Hi MichaelWion,

Unfortunately I can’t se your image but You can do two things to workaround this:

1. You can add method GetObject to You interface and simply return UObject\* from it for every class that will implement this interface:

This way You will get UObject\* so compiler won’t complain.

1. You can return UObject\* in the method instead of IMarionettistInterface\* so there is no problem with conversion.

You should remember that IInterface is just a pure C++ class so it’s not convertible to UObject\* in any way.

Here is a link to the documentation where You can check what are benefits of using UInterfaces.

[Interfaces](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Interfaces/index.html)

---

<div class="post-metadata">

**Author:** ![MichaelWion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MichaelWion](https://forums.unrealengine.com/u/MichaelWion)\
**Post date:** [September 20, 2015, 9:17am UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605/3 "2015-09-20T09:17:37Z")

</div>

I found that the following definition compiles:

```
static void FindMarionettistInScene(TEnumAsByte<EMarionettist> MarionettistName, bool &Error, TScriptInterface<IMarionettistInterface> &Interface);
```

---

<div class="post-metadata">

**Author:** ![MichaelWion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MichaelWion](https://forums.unrealengine.com/u/MichaelWion)\
**Post date:** [September 20, 2015, 9:18am UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605/4 "2015-09-20T09:18:32Z")

</div>

Thanks, see my answer to see how I solved it without a workaround

---

<div class="post-metadata">

**Author:** ![Fulezi](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/fulezi/32/813318_2.png) [@Fulezi](https://forums.unrealengine.com/u/Fulezi)\
**Post date:** [July 31, 2024, 3:55pm UTC](https://forums.unrealengine.com/t/returning-an-interface-from-c/332605/5 "2024-07-31T15:55:40Z")

</div>

This solution works in blueprint only setup but not for interface made in C++ and inherited in blueprint.

Unreal provides a `TScriptInterface<>` type which is designed to wrap an object as a specific interface. It’s the official suggested way to hold on to both the object reference safely and the interface it’s supposed to be presenting. You can use it like this:

```auto
TScriptInterface<IDoSomeThings> SomethingInstance;

```

When you want to use this stored reference, you call it using the overloaded `->` operator, i.e.:

```auto
int Num = SomethingInstance->GetNumberOfThings();

```

Simple in theory, but there’s one **major** problem with this, and that’s that `TScriptInterface<>` derives the interface pointer using `Cast<>`. And as we established earlier, `Cast<>` will **always return nullptr** if the instance in question is only implementing the interface at the Blueprint level, and none of its superclasses implement it in C++.

The thing is, you can still make `TScriptInterface<>` work in this scenario, by instead calling:

```auto
int Num = IDoSomeThings::Execute_GetNumberOfThings(SomethingInstance.GetObject());

```

Quoted from here: [UE4 C++ Interfaces - Hints n Tips · SteveStreeting.com](https://www.stevestreeting.com/2020/11/02/ue4-c---interfaces---hints-n-tips/)
