虚幻引擎5 GAS开发俯视角RPG游戏 P06-09 玩家等级与战斗接口

一.创建一个变量Level:

1.英雄在PlayState类

Source/CC_Aura/Public/Player/CC_PlayerState.h:

(1)定义变量

cpp 复制代码
private:
	UPROPERTY(VisibleAnywhere, ReplicatedUsing = OnRep_Level)
	int32 Level;

(2)复制回调函数:

cpp 复制代码
	UFUNCTION()
	void OnRep_Level(int32 OldLevel);
	
cpp 复制代码
void ACC_PlayerState::OnRep_Level(int32 OldLevel)
{
	
}

(3)复制策略:

cpp 复制代码
public:
	// 用于声明哪些变量需要在服务器和客户端之间自动复制‌
	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
	
cpp 复制代码
void ACC_PlayerState::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME(ACC_PlayerState, Level);
}

2.敌人在角色类

cpp 复制代码
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Enemy Level")
	int32 Level;

二. 创建战斗接口:

1.创建接口函数:

Source/CC_Aura/Public/Interations/CC_CombatInterface.h:

cpp 复制代码
public:
	virtual int32 GetCharacterLevel();
cpp 复制代码
int32 ICC_CombatInterface::GetCharacterLevel()
{
	return 0;
}

2.角色基类继承接口:

cpp 复制代码
class CC_AURA_API ACC_CharacterBase : public ACharacter,public IAbilitySystemInterface, public ICC_CombatInterface

3.敌人类实现接口函数:

Source/CC_Aura/Public/Characters/CC_EnemyCharacter.h

cpp 复制代码
	/*ICC_CombatInterface接口函数:
	 *		1.获取角色等级
	 */
#pragma region ICC_CombatInterface
	
	virtual int32 GetCharacterLevel() override;

#pragma endregion
cpp 复制代码
int32 ACC_EnemyCharacter::GetCharacterLevel()
{
	return Level;
}

4.英雄类实现战斗接口函数:

cpp 复制代码
/*ICC_CombatInterface接口函数:
*		1.获取角色等级
*/
#pragma region ICC_CombatInterface
	
	virtual int32 GetCharacterLevel() override;

#pragma endregion
cpp 复制代码
int32 ACC_HeroCharacter::GetCharacterLevel()
{
	if (CCPlayerState == nullptr)
	{
		CCPlayerState = GetPlayerState<ACC_PlayerState>();	//获取玩家状态
	}
	return CCPlayerState->GetCharacterLevel();
}

显然,我们要先在CCPlayerState类里定义GetCharacterLevel()函数:

Source/CC_Aura/Public/Player/CC_PlayerState.h

cpp 复制代码
public:
	FORCEINLINE int32 GetCharacterLevel() const {return Level;}
	
相关推荐
Maple_land1 天前
Linux复习:冯·诺依曼体系下的计算机本质:存储分级与IO效率的底层逻辑
linux·运维·服务器·c++·centos
ue星空1 天前
UE核心架构概念
网络·c++·ue5
ShineWinsu1 天前
对于数据结构:堆的超详细保姆级解析——下(堆排序以及TOP-K问题)
c语言·数据结构·c++·算法·面试·二叉树·
ue星空1 天前
AActor前面为什么要加A? UObject前面为什么要加U?
ue5
_OP_CHEN1 天前
C++进阶:(五)map系列容器的全面解析
开发语言·c++·map·红黑树·stl容器·键值对·mapoj题
hetao17338371 天前
ZYZ28-NOIP模拟赛-Round4 hetao1733837的record
c++·算法
大米粥哥哥1 天前
c++ libcurl报错Send failed since rewinding of the data stream failed【已解决】
开发语言·c++·http·curl·rewind
woshimyc1 天前
ESP32连接ThingsCloud上传设备数据(智慧小灯)
c++·物联网
Maple_land1 天前
Linux复习:系统调用与fork
linux·运维·服务器·c++·centos
墨雪不会编程1 天前
C++的基础语法篇一 ——命名空间
开发语言·c++