虚幻引擎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;}
	
相关推荐
汉克老师8 小时前
GESP2025年3月认证C++五级( 第三部分编程题(1、平均分配))
c++·算法·贪心算法·排序·gesp5级·gesp五级
相信神话20219 小时前
3.2《酒魂》规则设计文档
游戏引擎·godot·2d游戏编程·godot4·2d游戏开发
智者知已应修善业11 小时前
【51单片机2个按键控制流水灯运行与暂停】2023-9-6
c++·经验分享·笔记·算法·51单片机
云泽80813 小时前
C++11 核心特性全解:列表初始化、右值引用与移动语义实战
开发语言·c++
AI进化营-智能译站14 小时前
ROS2 C++开发系列12-用多态与虚函数构建可扩展的ROS2机器人行为模块
开发语言·c++·ai·机器人
Morwit14 小时前
QML组件之间的通信方案(暴露子组件)
c++·qt·职场和发展
qeen8714 小时前
【数据结构】建堆的时间复杂度讨论与TOP-K问题
c语言·数据结构·c++·学习·
图码14 小时前
如何用多种方法判断字符串是否为回文?
开发语言·数据结构·c++·算法·阿里云·线性回归·数字雕刻
handler0114 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
zhouwy11314 小时前
Linux进程与线程编程详解
linux·c++