虚幻引擎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;}
	
相关推荐
念恒1230635 分钟前
Linux初识
linux·服务器·c++
旖-旎1 小时前
哈希表(存在重复元素)(3)
数据结构·c++·学习·算法·leetcode·散列表
apcipot_rain1 小时前
Python实战——蒙特卡洛模拟分析杀牌游戏技能收益
python·游戏·数学建模
计算机安禾1 小时前
【数据结构与算法】第39篇:图论(三):最小生成树——Prim算法与Kruskal算法
开发语言·数据结构·c++·算法·排序算法·图论·visual studio code
fish_xk1 小时前
c++内存管理
开发语言·c++·算法
chh5632 小时前
C++--内存管理
java·c语言·c++·windows·学习·面试
Yungoal2 小时前
C++ 标准模板库STL(Standard Template Library)
c++·哈希算法·散列表
我真不是小鱼2 小时前
cpp刷题打卡记录27——无重复字符的最长子串 & 找到字符串中所有字母的异位词
数据结构·c++·算法·leetcode
一直不明飞行2 小时前
C++:string,写法s.find(‘@‘) != s.end()是否有问题
开发语言·c++·算法
无限进步_2 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github