UE4的接口与蓝图

Using interfaces with C++ and Blueprints can be a little tricky when the interface is implemented purely in Blueprints. Currently, when a Blueprint implements an Interface the C++ InterfaceCast<> type cast does not work.

Interface Cast Method

auto MyInterface = InterfaceCast<IMyInterface>(ActorInstance);
if (MyInterface)
{
  // Other code
}

What you need to do instead is:

(Originally discovered by Lion032, https://answers.unrealengine.com/questions/43038/buginerfacecast-returns-null-for-blueprint-classes.html)

Implements Interface Method

if (ActorInstance->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
{
  // Other code
}

This will work for both C++ implemented interfaces and Blueprint implemented interfaces. From here, you can use the static Execute versions of the interface functions.

Header:

UINTERFACE(MinimalAPI)
class UMyInterface : public UInterface
{
  GENERATED_UINTERFACE_BODY()
};
 
class IMyInterface
{
  GENERATED_IINTERFACE_BODY()
 
public:
  UFUNCTION(BlueprintImplementableEvent)
  void MyInterfaceFunction();
};
Interface function example:
if (ActorInstance->GetClass()->ImplementsInterface(UMyInterface::StaticClass()))
{
  IMyInterface::Execute_MyInterfaceFunction(ActorInstance);
}
It is likely that the static Execute functions are generated by the Unreal Header Tool (UHT) only for interface functions that declare UFUNCTION(). This means that you can't make a virtual interface function which uses UFUNCTION() and intellisense won't pick them up.

More Complete Example (Passing Self as Reference):

MyInterface.H:

class AMyActor;
 
 
UINTERFACE(Category = "My Interface", BlueprintType, meta = (DisplayName = "My Interface"))
class MYMODULE_API UMyInterface : public UInterface {
	GENERATED_UINTERFACE_BODY()
};
 
class MYMODULE_API IMyInterface {
	GENERATED_IINTERFACE_BODY()
public:
	/// My Initialization Interface.
	UFUNCTION(Category = "My Interface", BlueprintNativeEvent, BlueprintCallable, meta = (DisplayName = "On My Interface Execute")
	void OnInitialized(const AMyActor* Context);
};
MyActor.H:
UFUNCTION(Category = "My Interface", BlueprintNativeEvent, BlueprintCallable, meta = (DisplayName = "On My Interface Call"))
void OnInitialized(const AMyActor* Context);

MyActor.CPP:

void AMyActor::OnInitialized_Implementation(const AMyActor* Context) {
 
	if (Context != this) {return;}
 
	TArray<AActor*>Interfaces;
	// Pick only Actors with Interface, instead of iterating whole World:
	UGameplayStatics::GetAllActorsWithInterface(this,UMyInterface::StaticClass(),Interfaces);
 
	for (const auto &Actor : Interfaces) {
 
		// Try to Execute on C++ layer:
		const auto &Interface = Cast<IMyInterface>(Actor);
		if (Interface) {Interface->Execute_OnInitialized(Actor,Context);} else
 
		// Else, Execute Interface on Blueprint layer instead:
		if (Actor->GetClass()->ImplementsInterface(UMyInterface::StaticClass())) {
			IMyInterface::Execute_OnInitialized(Actor,Context);
		}
 
	}
 
}
 
void AMyActor::BeginPlay() {
	Super::BeginPlay();
 
	// Fire off the Native Event, which is going to be received by all the other Actors:
	Execute_OnInitialized(this,this);
 
}

Notes: Example above is implemented with old GENERATED_UCLASS_BODY() instead of new GENERATED_BODY() one. If you want your Actor to receive C++ Interface calls instead of from Blueprint layer, your Actor must inherit from both AActor class and your Interface class, like so:

class MYMODULE_API AMyActor : public AActor, public IMyInterface {...}

Update and pointer to more detailed discussion

Thanks for this wiki; I think it is essential on getting interfaces to work properly. I wrote a longer discussion of what I think is going on with Blueprint and C++ interfaces at https://answers.unrealengine.com/questions/214147/grand-unified-cblueprint-cast-interface-explanatio.html which I thought readers of this topic might find useful. -Xarol

注: 转载这篇文章主要是可以这么使用:C++定义接口,蓝图类通过Blueprint Setting添加接口,然后C++或者蓝图拿到蓝图实体或者通过Class拿到CDO然后调用接口可以调用到蓝图类实现的接口,即使你根本不知道这个蓝图类是什么。

转载地址:https://wiki.unrealengine.com/Interfaces_And_Blueprints


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值