1.打开UE引擎,选择接口 C++类,命名为CombatInterface
打开CombatInterface.h,创建两个方法接口
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "CombatInterface.generated.h"
// This class does not need to be modified.
UINTERFACE(MinimalAPI,NotBlueprintable, BlueprintType, meta = (CannotImplementInterfaceInBlueprint))
class UCombatInterface : public UInterface
{
GENERATED_BODY()
};
/**
*
*/
class SIMPLECOMBAT_API ICombatInterface
{
GENERATED_BODY()
// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:
//用于连击攻击
UFUNCTION(BlueprintCallable, Category = "Combat")
virtual void CombatAttack(const FName& InKey) {};
//用于获取连击检测结构体
virtual struct FSimpleCombatCheck* GetSimpleCombatInfo() { return NULL;}
};
2.打开CharacterBase.h,继承这个接口
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "CombatInterface.h"//包含接口头文件
#include "MMOARPGCharacterBase.generated.h"
UCLASS()
class MMOARPGGAME_API AMMOARPGCharacterBase : public ACharacter,public ICombatInterface//继承接口
{
GENERATED_BODY()
public:
void NormalAttack(const FName& InKey);
//继承至接口的CombatAttack方法
virtual void CombatAttack(const FName& InKey);
//继承至接口的GetSimpleCombatInfo方法
virtual FSimpleCombatCheck* GetSimpleCombatInfo();
打开CharacterBase.cpp,实现这个方法
void AMMOARPGCharacterBase::NormalAttack(const FName& InKey)
{
GetFightComponent()->NormalAttack(InKey);
}
void AMMOARPGCharacterBase::CombatAttack(const FName& InKey)
{
NormalAttack(InKey);
}
FSimpleCombatCheck* AMMOARPGCharacterBase::GetSimpleCombatInfo()
{
return GetFightComponent()->GetSimpleCombatInfo();
}