# Declare, implement and call an interface in C++

**URL:** <https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038>\
**Category:** UI\
**Tags:** interface, interfaces, unreal-engine, CPP, question\
**Created:** [August 3, 2016, 8:00pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038 "2016-08-03T20:00:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_213579db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_213579db](https://forums.unrealengine.com/u/anonymous_user_213579db)\
**Post date:** [August 3, 2016, 8:00pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/1 "2016-08-03T20:00:29Z")

</div>

I have an interface declared and called in C++. There are implementations in C++ and blueprints.

I am following the documentation ([Interfaces in Unreal Engine | Unreal Engine 5.2 Documentation](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Interfaces)), which doesn’t have an essencial part (how do I call the method?).

So, I declared and implemented as the documentation says, and I am calling using the following code:

```
IButtonControlledUI::Execute_OnFocusReceivedByFocusManager(FocusedObject, this);

```

where IButtonControlledUI is my class, OnFocusReceivedByFocusManager is the method and FocusedObject is the owner.

**It’s working in blueprints, but not in C++**. The implemenation is not being called. The **generated code** (see the screenshot bellow) is not calling the OnFocusReceivedByFocusManager\_Implemenation method. What must I do?

 ![101294-screnshot.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/b/b/6bbdc95c82c08cbdfba204c4eab2144847e5692a.png)

Fore more details, these are the declaration and implementation:

The interface:

```
UINTERFACE(Blueprintable)
class UButtonControlledUI : public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class IButtonControlledUI
{
	GENERATED_IINTERFACE_BODY()

public:
	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Button Controlled UI")
	void OnFocusReceivedByFocusManager(AFocusManager* FocusManager);

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Button Controlled UI")
	void OnFocusLostByFocusManager(AFocusManager* FocusManager);
};

```

The class that implements the interface (.h):

```
UCLASS()
class SQUAD51_API UActionMenuItem : public UTextBlock, public IButtonControlledUI
{
	GENERATED_BODY()
	
public:
	virtual void OnFocusReceivedByFocusManager_Implementation(AFocusManager* FocusManager) override;
	virtual void OnFocusLostByFocusManager_Implementation(AFocusManager* FocusManager) override;
};

```

The class that implements the interface (.cpp, these are the methods that are’t called):

```
void UActionMenuItem::OnFocusReceivedByFocusManager_Implementation(AFocusManager* FocusManager)
{
	SetOpacity(1.f);
}

void UActionMenuItem::OnFocusLostByFocusManager_Implementation(AFocusManager* FocusManager)
{
	SetOpacity(0.5f);
}

```

And this is how I called: (FocusedObject appears to be correct, I checked it):

```
if (FocusedObject->GetClass()->ImplementsInterface(UButtonControlledUI::StaticClass()))
				IButtonControlledUI::Execute_OnFocusReceivedByFocusManager(FocusedObject, this);

```

---

<div class="post-metadata">

**Author:** ![DennyR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DennyR](https://forums.unrealengine.com/u/DennyR)\
**Post date:** [August 3, 2016, 8:11pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/2 "2016-08-03T20:11:54Z")

</div>

I am not 100% sure on this, since I never tried it, but I think that you cannot use UObject-\>FindFunction(FName Name) on compile time.

However, you can just Cast\<\>() the object to your interface:

`IYourInterface* TheInterface = Cast<>(YourObjectThatImplementsTheInterface);`

_Put IYourInterface inside these \<\> brackets, for some reason answerhub markup doesn’t allow text inside these brackets_

A more detailed example can be found in [Ramas Wiki Tutorial](https://wiki.unrealengine.com/Interfaces_in_C%2B%2B)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_213579db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_213579db](https://forums.unrealengine.com/u/anonymous_user_213579db)\
**Post date:** [August 3, 2016, 8:33pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/3 "2016-08-03T20:33:36Z")

</div>

DennyR, thanks for the reply. I didn’t use “UObject-\>FindFunction”, it is part of the generated code. I just put the screenshot to show what the engine is doing.

I think the Rama’s solution doesn’t work with blueprints. ☹

I was trying to do a solution that works with both blueprints and C++. In fact, blueprints are working, just the C++ implemenation is not being called at all.

---

<div class="post-metadata">

**Author:** ![DennyR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DennyR](https://forums.unrealengine.com/u/DennyR)\
**Post date:** [August 3, 2016, 8:38pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/4 "2016-08-03T20:38:50Z")

</div>

Ramas solution does work with blueprint, you just have to make it Blueprintable, like you already have in your example.

```
UINTERFACE(Blueprintable)
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_213579db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_213579db](https://forums.unrealengine.com/u/anonymous_user_213579db)\
**Post date:** [August 3, 2016, 9:24pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/5 "2016-08-03T21:24:32Z")

</div>

But I tried Cast and it returned returns null if I pass the blueprint object. And Ramas solution uses virtual methods, not allowed if I declare a BlueprintImplamentableEvent or BlueprintNativeEvent.

---

<div class="post-metadata">

**Author:** ![DennyR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DennyR](https://forums.unrealengine.com/u/DennyR)\
**Post date:** [August 3, 2016, 10:03pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/6 "2016-08-03T22:03:42Z")

</div>

Hm, I never had any issues with interfaces in c++. So not sure why your cast returns NULL. I never tried to use a C++ interface in a blueprint class though, so I can’t tell what the issue is.  
The BlueprintImplamentableEvent or BlueprintNativeEvent not being allowed to be virtual might be an issue though. It might actually be a design flaw.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_213579db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_213579db](https://forums.unrealengine.com/u/anonymous_user_213579db)\
**Post date:** [August 3, 2016, 10:19pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/7 "2016-08-03T22:19:56Z")

</div>

Well, after some recompiles, editor restarts and more recompiles, I think it is magically working now…

Just to be clear:

- To declare and implement the interface in C++, follow the documentation: [Interfaces in Unreal Engine | Unreal Engine 5.2 Documentation](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Interfaces)

- To call the interface in C++, use the following example:

- It works with both C++ or blueprints implementations.

- The Rama’s tutorial may work well with C++, but it doesn’t support blueprints. 😉

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_213579db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_213579db](https://forums.unrealengine.com/u/anonymous_user_213579db)\
**Post date:** [August 3, 2016, 10:23pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/8 "2016-08-03T22:23:55Z")

</div>

The blueprint object doesn’t work the same way C++ object does. This is why the cast doesn’t work. When you configure the blueprint to implement some interface, it doesn’t inherits the interface class.

Anyway, I think it’s working now… I tried to recompile and restart the editor many times before, but I was doing something wrong, I think… because I done it againg and now it’s working…

---

<div class="post-metadata">

**Author:** ![RotemS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RotemS](https://forums.unrealengine.com/u/RotemS)\
**Post date:** [May 4, 2017, 9:40am UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/9 "2017-05-04T09:40:57Z")

</div>

Thanks, totally worked for me!

---

<div class="post-metadata">

**Author:** ![TysonEX](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TysonEX](https://forums.unrealengine.com/u/TysonEX)\
**Post date:** [May 4, 2017, 2:31pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/10 "2017-05-04T14:31:25Z")

</div>

Worked for me as well, thank you!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_13399bb2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_13399bb2](https://forums.unrealengine.com/u/anonymous_user_13399bb2)\
**Post date:** [March 27, 2019, 9:10pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/11 "2019-03-27T21:10:17Z")

</div>

The code exactly as written in the “documentation” does not compile. The virtual decorator throws:

```
error : BlueprintImplementableEvents in Interfaces must not be declared 'virtual'

```

Gotta love when the example itself does not compile.

---

<div class="post-metadata">

**Author:** ![VincentJun](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@VincentJun](https://forums.unrealengine.com/u/VincentJun)\
**Post date:** [April 29, 2019, 6:54am UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/12 "2019-04-29T06:54:22Z")

</div>

Pure virtual function can’t Implementable at blueprint , how to do resolve it? thanks!!!

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [April 29, 2019, 5:35pm UTC](https://forums.unrealengine.com/t/declare-implement-and-call-an-interface-in-c/366038/13 "2019-04-29T17:35:49Z")

</div>

if that is a question  
you can’t there are no pure virtual functions that are implementable in blueprints

you have two options depending on what you want to do:

1. you want to override the function in C++ and Blueprint  
use **BlueprintNativeEvent** and give it an empty function body  
UFUNCTION(BlueprintNativeEvent)  
void Something();  
in cpp:  
void Something\_Implementation() {}

2. it’s only used and overridden in blueprints use **BlueprintImplementableEvent** , which doesn’t force you to do anything else in C++  
UFUNCTION(BlueprintImplementableEvent)  
void Something();
