Working with C++ Interfaces and Blueprints

Hi there guys, i’m making steady process.

Right now i’m crafting code on c++ and i found this issue. I created a new c++ interface (using Ramma’s explanation) on

I created a c++ interface called InteractableInterface wich has a UInteractableInterface and a IInteractableInterface.
The IInterface part looks like this.

class MYPROJECT_API IInteractableInterface

{

GENERATED_IINTERFACE_BODY()

public:

UFUNCTION(BlueprintNativeEvent)

void Interact();

UFUNCTION(BlueprintNativeEvent)

void OnStartFocus();

UFUNCTION(BlueprintNativeEvent)

void OnEndFocus();

};

This works excelent i can implement this interface on blueprints or on c++ classes with no trouble at all…
The problem comes when i need to trigger this actions. My PlayerController right now uses linecast to check if an actor using this interface is on focus,this works, using

bool isInteractable = UKismetSystemLibrary::DoesImplementInterface(outHit.GetActor(),UInteractableInterface::StaticClass());

My main idea was to store a reference for the Interactable Actor so the team can call it on blueprint or C++ code.
Here’s where i found the issue, how would i procede from here? casting the Actor to store it always return nulls per:

//Linecast result being cast to IInteractableInterface Fails

IInteractableInterface *focusActor = Cast(outHit.GetActor());

And even if it worked, i think i can’t show IInteractableInterface to Blueprints.
My question is… being a programmer and having experience using interfaces… how should i proceed? should i store this on an Actor reference only? does this implementation works if i implement the interface on C++ OR on blueprint? or do i have to use different use cases for each one?
Bye!

Hello,

Once you get your actor (from UGameplayStatics::GetAllActorsWithInterface or another way) and check if it implements the interface, you have to call an auto-generated function using the naming convention “Execute_[name of your function]”.
For example, to call your Interact function, you will have to do :
IInteractableInterface::Execute_Interact(outHit.GetActor());
If you have multiple parameters, you just have to add them after this first one, that is always the object on which you call the function.

I’m used to store this kind of variables (as i want to make them public to blueprints) as the interface (so the variable is well encapsulated). But this is failing as i cannot cast the actor (which implements via blueprint the interface, not via c++) to the interface (not IInterface nor UInterface). Should i just store this as an actor and call execute_*?

thanks!

Not sure to understand everything you want but the cast may fail because you use Cast. For interfaces, you must use “InterfaceCast”

Nope, it’s the same, InterfaceCast was replaced with Cast… but it always returns null… I read that it’s because the Actor isn’t implementing the interface via C++ but via blueprint… so C++ can’t read the headers and get the IInterface… If that’s the case i don’t know which design choice to take… use only actors (and avoid casting to the interface) or implement every c++ interface in C++ and make the blueprint inherit from them (so CAST would work) and use the Execute_* methods

Ah yes, you are right ^^ Besides, it is since a long time… :

DEPRECATED(4.6, "InterfaceCast is deprecated, use Cast or dynamic_cast instead.")

So well, it should work :confused: I don’t have more suggestions in head.
If the DoesImplementInterface returns true, the cast should work…

PS : Just in case, can you try this ? :
IInteractableInterface *focusActor = Cast(outHit.GetActor());

Nope i was already using CAST like this…

IInteractableInterface *focusActor = Cast<IInteractableInterface>(outHit.GetActor());

But it always returns nullptr

I saw in your other new thread that you inherited from blueprint. That’s why it doesn’t work.
Blueprints are not “real” classes, so they don’t “really” inherit from the interface and finally, cannot be casted.
You will have to save your variable as AActor* I think.

If that’s the case what’s the prefered way of programming this? creating interfaces in C++ and implementing them ONLY on C++?