Download

Need help with interfaces

Hello,

I’m a Unity developer trying to dive into Unreal.

To get an interface type as a UPROPERTY I ended up with this:


	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Trigger Target")
	TScriptInterface<class ITriggerableInterface> TriggerableObject;

But: I won’t be able to pick an actor (which implements this interface) from the scene.
All I get is an asset path textfield in the details panel.

Unreal_Trigger_Path.png

I wanted to go the interface way to use my trigger object for all sorts of other objects and not just “doors” like I did before:


	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	class ADoor* Door;	

Which would work and look like this in the details panel:

9d1a78abca96acaba26afa465255a70eccc17f6a.png

I fear that I reached a dead end now… :-/.
Still I wanted to ask whether it’s possible to achieve what I wanted: An editor slot to reference all UObjects which implement my c++ interface.
Should I have used an abstract AActor instead?

Not an expert on editor UI but I believe this all keys back to UCLASS objects (RTTI that a raw C++ interface or forward declared class will lack).

As a style preference, I generally rig a class type which is used later to create an instance.
Take a look at the usage of TSubClassOf at Can I declare a variable to hold a Widget BP Reference? - C++ Gameplay Programming - Unreal Engine Forums

Not asked, but like Unity, grid view properties can be be customized (once the object type is known) Details Panel Customization | Unreal Engine Documentation
A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums

I believe this is still unsupported, and your best approach for now is to use an AActor property, and do some manual checking for interface compatibility. Not ideal I know.

This isn’t too hard to do on the C++ level if you create a detail customization and set up the dropdown control yourself. I did something very similar a while ago. My impression is that more support for interfaces in the editor is gradually being added to the codebase, but it’s still experimental so hasn’t been put into use yet.

One other thing to note, I’m pretty sure TScriptInterface only works with classes that have inherited the interface natively. If you add the interface in a blueprint, you won’t be able to hold an instance of that blueprint in a TScriptInterface variable. Another reason to go the AActor route.

Hey psychogony, hey kamrann,

thanks a lot for your replies.
Well, I already got the feeling that interfaces in Unreal are rather experimental at the current state.
Thanks for confirming this and for the advice to rather inherit from AActor.

I wanted to enforce overriding of some methods by using an interface because the PURE_VIRTUAL macro you could use in class methods doesn’t seem to throw an error at compile time.

So there really doesn’t seem to be clean solution. At this point of time.

Assuming your interface functions are UFUNCTIONS, they can’t be truly pure virtual anyway, you’d be using the same PURE_VIRTUAL macro.
I believe that macro should generate an error if not overridden, but it will be in the form of runtime log output in the editor output window.

Indeed… just tried it out again. PURE_VIRTUAL will make the editor crash when calling the method, eventually printing an error above the call-stack that a pure virtual method wasn’t overridden.
Semi-cool.