Ue4_C++接口_转载
转载官方文档 https://docs.unrealengine.com/4.27/zh-CN/ProgrammingAndScripting/GameplayArchitecture/Interfaces/
接口有助于确保一组(可能)不想关的类实现一组通用函数。
例如,某个游戏的系统,系统有一个触发器体积可以激活陷阱/警示敌人/向玩家奖励点数
可以对陷阱 敌人和点数奖励器 执行ReactedTrigger 函数实现,然后陷阱可能派生 AActor,敌人可能派生自专门的APawn或ACharacter 子类,点数可能派生自UDataAsset,无Uobject之外共同上级,这种情况推荐使用接口
ReactToTriggerInterface.h #pragma once #include "ReactToTriggerInterface.generated.h" UINTERFACE(MinimalAPI, Blueprintable) class UReactToTriggerInterface : public UInterface { GENERATED_BODY() }; class IReactToTriggerInterface { GENERATED_BODY() public: /** 在此处添加接口函数声明 */ };
前缀为U 的类不需要任何构造或者其他函数,前缀为I的类包含所有的接口函数,且此类将被其他类继承,如想让蓝图实现当前接口,需要Blueprintable 说明符
在c++ 中实现接口
Trap.h
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ReactToTriggerInterface.h"
#include "Trap.generated.h"
UCLASS(Blueprintable, Category="MyGame")
class ATrap : public AActor, public IReactToTriggerInterface
{
GENERATED_BODY()
public:
/** Add interface function overrides here. */
}
仅C++ 的接口函数
ReactToTrigger.h
public:
virtual bool ReactToTrigger();
ReactToTrigger.cpp
bool IReactToTriggerInterface::ReactToTrigger()
{
return false;
}
当一个Actor类实现接口后,可以创建并实现一个针对该类的覆盖
Trap.h
public:
virtual bool ReactToTrigger() override;
Trap.cpp
bool ATrap::ReactToTrigger()
{
return false;
}
这些c++接口未添加表示符 对蓝图不可见
蓝图可调用接口函数
创建蓝图可调用的接口函数,必须带BlueprintCallable ,必须使用BlueprintImplementableEvent
或BlueprintNativeEvent 且函数不能为虚拟的
ReactToTrigger.h
public:
/**只能在蓝图中实现的React To Trigger版本。*/
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category=Trigger Reaction)
bool ReactToTrigger();
ReactToTrigger.h
public:
/**可以在C++或蓝图中实现的React To Trigger版本。*/
UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category=Trigger Reaction)
bool ReactToTrigger();
注:NativeEvent 方法在实现需要加 _Implementation() 后缀
确定是否实现了接口
bool bIsImplemented = OriginalObject->GetClass()->ImplementsInterface(UReactToTriggerInterface::StaticClass()); // 如果OriginalObject实现了UReactToTriggerInterface,则bisimplemated将为true。
bIsImplemented = OriginalObject->Implements<UReactToTriggerInterface>(); // 如果OriginalObject实现了UReactToTrigger,bIsImplemented将为true。
IReactToTriggerInterface* ReactingObject = Cast<IReactToTriggerInterface>(OriginalObject); // 如果OriginalObject实现了UReactToTriggerInterface,则ReactingObject将为非空。
转到到其他虚幻类型
支持从一个接口转换到另外一个接口,在适当的情况下从一个接口转换到另外一个虚幻类型
IReactToTriggerInterface* ReactingObject = Cast<IReactToTriggerInterface>(OriginalObject); // 如果接口被实现,则ReactingObject将为非空。
ISomeOtherInterface* DifferentInterface = Cast<ISomeOtherInterface>(ReactingObject); // 如果ReactingObject为非空而且还实现了ISomeOtherInterface,则DifferentInterface将为非空。
AActor* Actor = Cast<AActor>(ReactingObject); // 如果ReactingObject为非空且OriginalObject为AActor或AActor派生的类,则Actor将为非空。
自定义接口简单实现
ReactToTriggerInterface.h
#pragma
#include "ReactToTriggerInterface.generated.h"
UINTERFACE(BlueprintType)
class UReactToTriggerInterface :public UInterface
{
GENERATED_BODY()
};
class IReactToTriggerInterface
{
GENERATED_BODY()
public:
//pure c++
virtual bool ReactToTriggerFunc_pure();
//NativeEvent
UFUNCTION(BlueprintCallable,BlueprintNativeEvent)
bool ReactToTriggerNativeFunc();
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
bool ReactToTriggerBlueprintImpFunc();
};
pawn.h
class ATrace_ProjectCharacter : public ACharacter, public IReactToTriggerInterface
{
GENERATED_BODY()
bool ReactToTriggerFunc_pure();
UFUNCTION(BlueprintCallable,BlueprintNativeEvent)
bool ReactToTriggerNativeFunc();
UFUNCTION(BlueprintCallable, BlueprintImplementableEvent)
bool ReactToTriggerBlueprintImpFunc();
};
pawn.cpp
bool ATrace_ProjectCharacter::ReactToTriggerFunc_pure()
{
return false;
}
bool ATrace_ProjectCharacter::ReactToTriggerNativeFunc_Implementation()
{
return false;
}