Getting self reference using c++

Spending all day looking at my c++ codes and unreal engine errors yesterday, I finally narrowed down to 2 lines of code that might need some changes. I believe this question doesn’t require my entire c++ code.

This is from the player’s .cpp file.

if(GetOwner()->GetClass()->ImplementsInterface(UCInterfaceActorManager::StaticClass()))
					ICInterfaceActorManager::Execute_ActorInRange(this,CurrentLoop);

This is ActorInRange function from the interface’s .h file.

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "ManageActor")
		void ActorInRange(AActor* Actor);

What I intended was:
If player(self) implements CInterfaceActorManager, call ActorInRange function.
This function is in the CInterfaceActorManager interface.

But I’m not sure GetOwner()->GetClass() gives me ‘Self’(in blueprint terms).
Implementation of interfaces is all done by blueprints.

In cpp, self is not required to call the functions withing the same class.
Just call the function directly.

if (ImplementsInterface(%InterfaceNameHere%))

1 Like

As I tried to delete GetOwner()->GetClass()-> and make
if (ImplementsInterface([MyInterfaceName])), the error line says me to include these.

#include <ClassViewer/Private/UnloadedBlueprintData.h>
#include <ClassViewerFilter.h>
#include <Programs/UnrealHeaderTool/Private/UnrealTypeDefinitionTool.h>
#include <Audio/AudioWidgetSubsystem.h>
#include <d3d11shader.h>
#include <d3d12shader.h>

Am I supposed to do this?

I don’t think so… maybe try with UCInterfaceActorManager::StaticClass() as you did before?

1 Like

Sorry it didn’t work. Maybe I have to search for more tutorials to learn the basics before I ask anything.

ImplementsInterface is UClass member function.
UClass::ImplementsInterface

Use UObject::StaticClass function to get instance associated with given UObject-based class.

It looks like you’re trying to call the function on the interface class directly, and not the object? Maybe try this

if(GetOwner()->GetClass()->ImplementsInterface(UCInterfaceActorManager::StaticClass()))
{
     UCInterfaceActorManager im = Cast<UCInterfaceActorManager>(GetOwner());
     im->Execute_ActorInRange(this,CurrentLoop);
}					

There is Implements<T> shortcut nowadays in UObject.

Your original syntax is the proper one for blueprint-implemented interfaces, you must use Execute_ prefix method.

The one thing that is wrong is, you are checking if the OWNER implements interface, but calling interface function on this object.
Either you want to call function on the owner :

if (GetOwner()->Implements<UCInterfaceActorManager>())
    ICInterfaceActorManager::Execute_ActorInRange(GetOwner(), CurrentLoop);

Or you want to call function on the current object (self) :

if (Implements<UCInterfaceActorManager>())
    ICInterfaceActorManager::Execute_ActorInRange(this, CurrentLoop);