UINTERFACE(MinimalApi)
class UMyInterface : public UInterface
{
GENERATED_UINTERFACE_BODY()
};
class IMyInterface
{
GENERATED_IINTERFACE_BODY()
public:
UFUNCTION()
virtual void MyFunc();
};
That already works. Now I have a class that inherits from both: Actor and MyInterface:
UCLASS()
class AMyClass : public AActor, public IMyInterface
{
GENERATED_UCLASS_BODY()
...
Now to the actual problem. I’d like to have a variable of the type IMyInterface* (in some other class):
UPROPERTY(BlueprintReadWrite, Category = "My category")
IMyInterface* MyActor;
MyActor should be spawned at some point later in the game by World->SpawnActor(…). However what I get is a error saying: “UPROPERTY pointers cannot be interfaces”.
I feel like I’m misunderstanding smth. basic here. Is this not a way to work with interfaces? What’s the proper way of implementing a case like this?
I’m not sure what the underlying implementation of UInterfaces are, but I suspect it would be an abstract class, therefore you cannot create an instance of one. You can only check whether a specific instance implements that interface.
I couldn’t accept this myself at first either. The first thing you think is of course what did I misunderstand, this must be possible to do in a modern game engine in 2014. But unfortunately I don’t think there is a solution to this at the moment.
The proper unreal engine way to implement this if you need an UObject or Actor is at the moment to use dynamic casts. Just look at their code examples or even the game engine code itself.
It’s kinda weird but they really missed the main advantages of interfaces with the current design. If you agree with me you can support my request at their forum or write your own.