Having Errors while trying to check if an Actor implements an interface. StaticClass is not a member

https://forums.unrealengine.com/core/image/gif;base64

​​​​​ Hi everyone.

I’m looking into this for hours now and couldn’t figured it out. Also couldn’t find anything about it on forums or answerhub. I also tried to open a question on Answerhub but I kept having an error. I really need your help.

In more detail:
I have created an Interface class so I could get an integer, that represents the FocusPriority, from different classes. But the problem is when I want to check if the class implements the Interface, the functions “Implements()” and “ImplementsInterface()” can’t call StaticClass() function on my interface class. The error says it’s not a member of the class. But as far as I know this function is added by GENERATED_BODY() Macro. I must have been missing a point; but what?

I’ve created all of my service and revert my changes to recreate everything step by step. Yet the problem persists. I don’t know what to do. I need this Interface to work with Blueprints too; at least for now.

Did any of you had this error before? Please help me. This took longer than it should already

We use interfaces extensively and just cast to the interface in situations like that.

Not sure that interface check is worth doing since you have to cast the object anyway to do something with it.



// for each actor in world
for (TActorIterator<AInteractiveActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
    IInteractiveBase* interactiveActor = Cast<IInteractiveBase>(*ActorItr);
    if (interactiveActor != nullptr)
    {
          // do something with object
    }
}


Did you have included interface header file to your h/cpp file ?

UPD:
Also, you probably need static class from U* prefix name interface class, not from I* at ImplementsInterface func.

7 Likes

I added my header files. BUT OMG… How did I missed that… Thank so much for pointing that out for me. It’s actually obvious to use the ‘U’ prefixed class but it was 4 am when I had that problem. I’m going to test it when I arrive at home and update this thread.

1 Like

As an tested update. The suggestion that is below and given by Shmoopy1701was the solution I’ve found before and worked. Using the ‘U’ prefixed class to get static class didn’t worked. Also after experimenting with it Casting to interface class is also makes the function call easier for us. Thank you Shmoopy. Didn’t see your reply in the morning, sorry for that.

In internet I saw other tutorials suggesting to use the ‘U’ prefixed class but didn’t work for me. Anyway, thank both of you for replying and helping me finding my way out of this.

Interfaces can be implemented two ways in Unreal Engine…
Either a C++ Interface or a UInterface sub-object; so you must check for both:



const auto & MyInterface = Cast<IMyInterfaceClass>( MyActor );

if (MyInterface)
{

    MyInterface->Execute_OnSomethingHappened(MyActor);

}
else
if ( MyActor->GetClass()->ImplementsInterface(UMyInterfaceClass::StaticClass()) )
{

    IMyInterfaceClass::Execute_OnSomethingHappened(MyActor);

}


Execute_xxx is a generated method from the BlueprintNativeEvent you declared in your IMyInterfaceClass.

2 Likes

Thanks for the tip :slight_smile: But since I’m sure it’s a UInterface do I need to check for both? If I do why?

Would really appreciate if you could explain. I’m trying to know better ways to use C++.

1 Like

Your Interface must be like this

class UMyInterface : public UInterface
{
GENERATED_BODY()
//this is UE4’s class
};

class ABC_API IMyInterface
{
GENERATED_BODY()
//this is where your interface function is
public:
UFUNCTION(BlueprintNativeEvent)
void MyFunc1();
}

The thing is you should use UMyInterface to test instead of IMyInterface like this:
if(MyActor->Implements< UMyInterface >())

NOT THIS:
if(MyActor->Implements< IMyInterface >())

This thing confuse me all a day :face_with_symbols_over_mouth:

1 Like