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(); } } }