I have a pure virtual interface IResettable:
UINTERFACE(MinimalAPI, Blueprintable)
class CHRONO_API UResettable : public UInterface
{
GENERATED_BODY()
};
class CHRONO_API IResettable
{
GENERATED_BODY()
public:
virtual void setReset() = 0;
};
and 2 other interfaces that derive from it, IPausable and ISpeedable.
UINTERFACE(MinimalAPI, Blueprintable)
class CHRONO_API UPausable : public UInterface
{
GENERATED_BODY()
};
class CHRONO_API IPausable : public IResettable
{
GENERATED_BODY()
public:
virtual void setPause() = 0;
};
The logic is that an actor that be paused or sped-up (some property of the game i’m developing), must also be able to be reset back to its normal state. By nesting the interfaces, the actor has no need to inherit IResettable, he can just inherit IPausable and/or ISpeedable and implement all the necessary methods (you can’t even compile the code if you forget to implement setReset()
.
UCLASS()
class CHRONO_API ABoxEntity : public AActor, public IPausable
{
GENERATED_BODY()
public:
void setPause() override;
void setReset() override;
};
Now, the issue comes on another part of the code. Some raycast hits one of these actors, and I want to check if they are IResettable, and from there, call setReset()
. The code I use to detect if the Actor is IPausable works fine.
if (hit.GetActor()->GetClass()->ImplementsInterface(UPausable::StaticClass()))
{
if (IPausable *pausable_actor = Cast<IPausable>(hit.GetActor()))
{
pausable_actor->setPause();
}
}
But the same code for IResettable does not work, and I think it’s because IResettable is a nested inheritance and neither ImplementsInterface
nor Cast
search for it.
if (hit.GetActor()->GetClass()->ImplementsInterface(UResettable::StaticClass()))
{
if (IResettable *resettable_actor = Cast<IResettable>(hit.GetActor()))
{
resettable_actor->setReset();
}
}
So, is this a design flaw from my side? Am I missing something on the interface declarations? Or is it a bug in ImplementsInterface
? I tried to provide an MVCE, but let me know if you need additional info.