Correct way to cast UInterface of Virtual functions? (NO UFUNCTIONS) C++

have an interface that does not contain UFUNCTIONS

UINTERFACE()
class UMyInteface : public UInterface
{
GENERATED_BODY()
};

class IMyInteface
{
GENERATED_BODY()

public:
virtual MyFunction();
};

1-If I use Cast< UClass >() I get a pointer to UMyInteface

UMyInteface* UPTR = Cast(UObj)

2-If I use Cast< Class >() get a compile error.

UMyInteface* IPTR = Cast< IMyInteface >(UObj)

3- If I use dinamic_cast< Class >() I get a compile error.

dynamic_cast< IMyInteface >(UObj)

4- If I use cast C style i not sure what will happend.

if (Owner->Implements< UMyInteface >()) return;
IMyInteface * MyIntefacePtr = (IMyInteface*)UObj;
if (MyIntefacePtr == nullptr) return;

In this case:
Which is the right way to do it? or the safest way?

Because I suppose that in this case the function must be called like this:

MyIntefacePtr->MyFunction();

and not in this other way.

IMyInteface::Execute_MyFunction( UPTR );

Thank you so much!!

Posts 5 and 7 might help you

Can’t tell you how this behaves with no UFUNCTION macro since it might not be able to reflect without one. Normally you declare your functions as UFUNCTION in the IMyInterface.

I believe that the only time you use

Implements<UGrabInteractionInterface>()

is when you need to test a blueprint implementation, but I might be wrong. If a method is not UFUNCTION it can’t be implemented by a blueprint.

Another way is to cast to it

else if (const IGrabInteractionInterface* OtherInterface = Cast<IGrabInteractionInterface>(OtherOwner)) {
	bCanBeGrabbed = OtherInterface->CanBeGrabbed(InPrimitiveComponent);
}

My post 5 on that link gives some info on when and why, but Chatouille in post 7 says things might be different in current versions of the engine.

2 Likes

Thank you very much for your help Roy!!
Very appreciated!!

2 Likes

It is not for blueprint… that’s why I don’t use UFUCTION…

But it wouldn’t let me derive from a pure class in C++… it had to be derived from UObject… However, being derived from UObject also has very useful benefits… so it’s fine… but I don’t need UFUCTIONS…

Thank you so much!! :heart:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.