Download

[Bug Report] BlueprintImplementableEvent and Interfaces don't work together


#pragma once
#include "TargetInterface.generated.h"
UINTERFACE(MinimalAPI)
class UTargetInterface :
	public UInterface
{
	GENERATED_UINTERFACE_BODY()
};

class ITargetInterface{
	GENERATED_IINTERFACE_BODY()
public:
        UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact"))
	void OnInteract();
}


It compiles fine and I can access the event in my blueprints but it will always throw an error when I call OnInteract(). It works fine if I put the BlueprintImplementableEvent in a normal class.

I think I had that problem too.
I assume you do something like this?

InterfaceOfMyActor= InterfaceCast<ITargetInterface>(Actor);
InterfaceOfMyActor->OnInteract();

Solution:
For some reason you are not allowed to call the function directly if implemented via an interface.
Instead do this (Note: First function argument is the Actor the Interface is casted from. Followed by all normal function args you want to send.):

InterfaceOfMyActor= InterfaceCast<ITargetInterface>(Actor);
InterfaceOfMyActor->ExecuteOnInteract(Actor);

I don’t know why you have to do it this way. But for me it works fine…

Edit - correction:
Forgot an underscore. It should be
InterfaceOfMyActor->Execute_OnInteract(Actor);

Many thanks, that seems to work.

A quicker way of doing this, without the need for an explicit cast, is to use the static helper function we automatically generate with interfaces. In your example, assuming that your interface is called “ITargetInterface”, you could simply call:

ITargetInterface::Execute_MyFunction(Actor);

which does the cast, and conditional call in one handy function!