C/C++教程

UE5/C++ 基于GAS的怪物AI 6.3.3 更新怪物角色属性、技能、血条显示

本文主要是介绍UE5/C++ 基于GAS的怪物AI 6.3.3 更新怪物角色属性、技能、血条显示,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.

打开AICharacter.cpp,通过读取角色属性数据表格更新角色的属性、技能

在BeginPlay中更新角色属性和技能以及角色名

void AMMOARPGAICharacter::BeginPlay()
{
	Super::BeginPlay();

	if (GetWorld())
	{
		//获取到GameState
		if (AMMOARPGGameState* InGameState = GetWorld()->GetGameState<AMMOARPGGameState>())
		{
			//通过GameState以及角色ID获取到对应角色属性数据表
			if (FCharacterAttributeTable* InAttributeTable = InGameState->GetCharacterAttributeTable(GetID()))
			{
				//将标签转为FName存储
				auto ToGamePlayNameTags = [&](const TArray<FGameplayTagContainer>& InTags, TArray<FName>& OutNames)
				{
					for (auto& Tmp : InTags)
					{
						OutNames.Add(*Tmp.ToString());
					}
				};

				//更新属性
				UpdateAttribute(InAttributeTable);

				TArray<FName>ComboAttack;
				ToGamePlayNameTags(InAttributeTable->ComboAttackTags, ComboAttack);

				
				if (GetWorld()->IsServer())
				{
					if (!GetController())
					{
						SpawnDefaultController();
					}

					TArray<FName>Skill;
					TArray<FName>Limbs;

					ToGamePlayNameTags(InAttributeTable->SkillTags, Skill);
					ToGamePlayNameTags(InAttributeTable->LimbsTags, Limbs);

					//更新技能
					RegisterGameplayAbility(ComboAttack, EMMOARPGGameplayAbilityType::GAMEPLAYABILITY_COMBOATTACK);
					RegisterGameplayAbility(Skill, EMMOARPGGameplayAbilityType::GAMEPLAYABILITY_SKILLATTACK);
					RegisterGameplayAbility(Limbs, EMMOARPGGameplayAbilityType::GAMEPLAYABILITY_LIMBS);
				}
			}
		}

		//客户端执行 名称获取
		if (GetLocalRole() != ENetRole::ROLE_Authority)
		{
			if (UUI_HealthState* InWidget = Cast<UUI_HealthState>(GetWidget()))
			{
				if (GetCharacterType() > ECharacterType::CHARACTER_NPC_RESIDENT)
				{
					if (AMMOARPGGameState* InGameState = GetWorld()->GetGameState<AMMOARPGGameState>())
					{
						if (FCharacterStyleTable* InStyleTable = InGameState->GetCharacterStyleTable(GetID()))
						{
							//更新角色名
							InWidget->SetCharacterName(InStyleTable->CharacterName);
						}
					}
				}
			}
		}
	}
}

2.

在Tick中不断更新角色等级和血条

void AMMOARPGAICharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (UUI_HealthState* InWidget = Cast<UUI_HealthState>(GetWidget()))
	{
		if (UMMOARPGAttributeSet* InAttributeSet = GetAttributeSet())
		{

			InWidget->SetLv(InAttributeSet->GetLevel());
			InWidget->SetHealth(InAttributeSet->GetHealth() / InAttributeSet->GetMaxHealth());

			LastHealth = InAttributeSet->GetHealth();
		}
	}
	
}

这篇关于UE5/C++ 基于GAS的怪物AI 6.3.3 更新怪物角色属性、技能、血条显示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!