Declare, implement and call an interface in C++

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

I am following the documentation (Interfaces | Unreal Engine Documentation), 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?

Fore more details, these are the declaration and implementation:

The interface:

class UButtonControlledUI : public UInterface

class IButtonControlledUI

	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):

class SQUAD51_API UActionMenuItem : public UTextBlock, public IButtonControlledUI
	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)

void UActionMenuItem::OnFocusLostByFocusManager_Implementation(AFocusManager* FocusManager)

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);

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

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. :frowning:

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.

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


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.

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.

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 | Unreal Engine Documentation

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

    if (MyObject->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
    IMyInterface::Execute_MyFunction(MyObject, MyArg);

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

  • The Rama’s tutorial may work well with C++, but it doesn’t support blueprints. :wink:

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…

Thanks, totally worked for me!

Worked for me as well, thank you!

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.

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

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
    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++
    void Something();