While I can test if a particular object implements an Interface in blueprints I cannot cast that object to that interface and use it as that interface. Under the circumstances I am overriding functions within the blueprints I will NOT have the concrete version of that object, they are being sent in as base objects such as AActor or UObject.
I’m building the system of interfaces to orchestrate how seemingly unrelated Actor objects communicate with each other. I’ve build the interface functions to take in any AActor object and test of that Actor has the appropriate interface for performing actions on. This works beautifully in C++. However Blueprint seems to be a different beast.
So Lets say I have two Interfaces:
class UInteractingAgent: public UInterface
{
GENERATED_UINTERFACE_BODY()
}
class IInteractingAgent
{
GENERATED_IINTERFACE_BODY()
void SetPendingInteractable(AActor* interactable) = 0;
void Pickup(AActor* pickup) = 0;
void Damage(int amount) = 0;
}
and
class UInteractableActor: public UInterface
{
GENERATED_UINTERFACE_BODY()
}
class IInteractableActor
{
GENERATED_IINTERFACE_BODY()
void IInteract(AActor* instigator) = 0
}
So InteractingActor performs a scan for anything in view. If it finds an actor that is any Actor in view and attempts to it to the InteractingAgent’s PendingInteractable (which is an Actor pointer) using the function SetPendingInteractable and passes that Actor into that function where it tests if that Actor has the IInteractableActor Interface.
void MyInteractingAgentCharacter::SetPendingInteractable(AActor * actor)
{
IInteractableActor * interactable = InterfaceCast<IInteractableActor>(actor)
if(interactable)
{
this->pendingInteractable = interactable;
}
}
Then if the user presses the interact button, the InteractingAgent’s Interact function is fired passing the PendingInteractable into that function where it is cast once again to an IInteractingAgent using an interface cast where it’s interact function is called passing the Pawn in as the instigator as an actor which it is then interface ast
void MyInteractingAgentCharacter::Interact()
{
if(this->pendingInteractable != NULL)
{
IInteractableActor * interactable = InterfaceCast<IInteractableActor>(this->pendingInteractable);
interactable->Interact(this)
}
}
void MyInteractableActor::Interact(AActor* instigator)
{
if(instigator)
{
IInteractingAgent * agent = InterfaceCast<IInteractingAgent>(instigator);
if(agent)
{
agent->Damage(10);
}
}
}
Basically I have certain functions that will be overriden in blueprint. Because the interactions can happen between Actor and Actor or Actor and Pawn, or UObject and Actor and Pawn. Since these are the basic Subclasses of all UE4 objects the interfaces expect the least common denominator of the three (somethings will never be on things that are not actors so the least common denominator is Actor). So in blueprint I need away to cast an UObject, Actor, or Pawn to the desired Interface to work with it… however that functionality does not seem to exist…
Technically I could Cast to every Base Class of my own within blueprints but then that somewhat defeats the entire purpose for using Interfaces…