Download

Can I use BlueprintImplementableEvent with an Interface as an argument?


UCLASS()
class AInteractableActor : public AActor , public ITargetInterface
{
	GENERATED_UCLASS_BODY()

	
public:
	UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact"))
	void OnInteract(AActor* clicked);
};

This compiles and works as expected but it would be nice if I could give it an interface as an argument instead of AActor like so.


UCLASS()
class AInteractableActor : public AActor , public ITargetInterface
{
	GENERATED_UCLASS_BODY()

	
public:
	UFUNCTION(BlueprintImplementableEvent, meta=(FriendlyName = "On Interact"))
	void OnInteract(ISomeInterface* clicked); // does not compile
};

Unfortunately this doesn’t compile.


1>E:\UnrealProjects\ThirdPersonProject\Intermediate\Build\Win64\Inc\MyProject3\MyProject3.generated.inl(47): error C2511: 'void AInteractableActor::OnInteract(const TScriptInterface<ISomeInterface> &)' : overloaded member function not found in 'AInteractableActor'
1>          e:\unrealprojects	hirdpersonproject\intermediate\build\win64\inc\myproject3\../../../../../Source/MyProject3/InteractableActor.h(13) : see declaration of 'AInteractableActor'
1>E:\UnrealProjects\ThirdPersonProject\Intermediate\Build\Win64\Inc\MyProject3\MyProject3.generated.inl(50): error C2352: 'UObject::FindFunctionChecked' : illegal call of non-static member function
1>          d:\program files\unreal engine\4.0\engine\source\runtime\coreuobject\public\uobject\UObject.h(543) : see declaration of 'UObject::FindFunctionChecked'
1>  -------- End Detailed Actions Stats -----------------------------------------------------------

Does someone know why it tries to convert my interface to an TScriptInterface<ISomeInterface>?

I now got it to compile.


#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 Test"))
	void OnInteract(ITargetInterface *clicked);
	void OnInteract(const TScriptInterface<ITargetInterface> &clicked);

But I have some problems using it


	auto t = InterfaceCast<ITargetInterface>(f.GetActor());
	if (t != nullptr)
		t->Execute_OnInteract(f.GetActor(),TScriptInterface<ITargetInterface>(t));

It seems Execute_OnInteract takes the TScriptinterface as a second argument but I am not sure how to create it.

I also tried



t->Execute_OnInteract(f.GetActor(),TScriptInterface<ITargetInterface>(t->GetUObjectInterfaceTargetInterface()));




But it doesn’t let me create a TScriptInterface from my ITargetInterface
Any help is greatly appreciated.


1>D:\Program Files\Unreal Engine\4.0\Engine\Source\Runtime\CoreUObject\Public\UObject\ScriptInterface.h(146): error C2440: 'initializing' : cannot convert from 'UObject *' to 'ITargetInterface *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>          D:\Program Files\Unreal Engine\4.0\Engine\Source\Runtime\CoreUObject\Public\UObject\ScriptInterface.h(126) : see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<UObjectType>(UObjectType *)' being compiled
1>          with
1>          
1>              InterfaceType=ITargetInterface
1>  ,            UObjectType=UObject
1>          ]
1>          D:\Program Files\Unreal Engine\4.0\Engine\Source\Runtime\CoreUObject\Public\UObject\ScriptInterface.h(126) : see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<UObjectType>(UObjectType *)' being compiled
1>          with
1>          
1>              InterfaceType=ITargetInterface
1>  ,            UObjectType=UObject
1>          ]
1>          E:\UnrealProjects\ThirdPersonProject\Source\MyProject3\MyProject3Character.cpp(173) : see reference to function template instantiation 'TScriptInterface<ITargetInterface>::TScriptInterface<UObject>(UObjectType *)' being compiled
1>          with
1>          
1>              UObjectType=UObject
1>          ]
1>          E:\UnrealProjects\ThirdPersonProject\Source\MyProject3\MyProject3Character.cpp(173) : see reference to function template instantiation 'TScriptInterface<ITargetInterface>::TScriptInterface<UObject>(UObjectType *)' being compiled
1>          with
1>          
1>              UObjectType=UObject
1>          ]

I finally got it


                auto t = InterfaceCast<ITargetInterface>(f.GetActor());
		if (t != nullptr){
			TScriptInterface<ITargetInterface> s = TScriptInterface<ITargetInterface>();
			s.SetInterface(t);
                        s.SetObject(f.GetActor());
			t->Execute_OnInteract(f.GetActor(),s);

That was a great adventure.

I am going to make a wiki entry for this.