虚幻引擎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;}
	
相关推荐
Dream it possible!2 小时前
LeetCode 面试经典 150_二分查找_在排序数组中查找元素的第一个和最后一个位置(115_34_C++_中等)
c++·leetcode·面试
月光下的麦克3 小时前
如何查案动态库版本
linux·运维·c++
小六子成长记3 小时前
【C++】:搜索二叉树的模拟实现
数据结构·c++·算法
汉克老师3 小时前
GESP2025年9月认证C++二级真题与解析(编程题1(优美的数字))
c++·算法·整除·枚举算法·求余·拆数
carver w4 小时前
MFC入门教程 最简版
c++·mfc
王老师青少年编程4 小时前
信奥赛C++提高组csp-s之倍增算法
c++·csp·信奥赛·csp-s·提高组·倍增算法·rmq
低频电磁之道4 小时前
编译C++的几种方式(MSVC编译器)
开发语言·c++
Zsy_0510034 小时前
【C++】类和对象(一)
开发语言·c++
世洋Blog4 小时前
Unity脚本生命周期(全)
unity·游戏引擎
是娇娇公主~5 小时前
工厂模式详细讲解
数据库·c++