How to have interface type as UPROPERTY?

How do I use an interface type as a UPROPERTY in c++? For example, in Blueprint the function GetAllActorsWithInterface takes an interface type as an argument. How could I make a C++ class which had a UFUNCTION like this or a UPROPERTY like this?


Hello, Xarol

In order to implement the desired functionality, consider using TScriptInterface smart pointer. The declaration will look something like this:

TScriptInterface<IUsableInterface> InterfacePointer;

The reason smart pointer is needed is that garbage collector is not interface-aware. Please note, that you need to assign it via the UObject type instead of the interface type. Also, nulling this pointer requires an explicit cast:

InterfacePointer = (IUsableInterface*)nullptr;

Hope that helps!

Thanks for the reply.

I think what you are saying is that if I want a property which can be an instance of an object which implements IUsableInterface then I can use TScriptInterface. Is that right?

What I actually want is to be able to pass an interface class like GetAllActorsOfInterface.

For example, I want to be able to do something like

UClass* DesiredInterfaceType;

and then be able to do something in C++ to all actors which implement that interface.

Maybe the clearest way to say it is as follows: “How would I implement GetAllActorsOfInterface in C++ so that the interface was specified in blueprint but the function was implemented in C++?”