虚幻引擎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;}
	
相关推荐
肆忆_9 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星13 小时前
虚函数表:C++ 多态背后的那个男人
c++
xiezhr15 小时前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
爱搞虚幻的阿恺4 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎