GKLPlayerState.cpp
Click a line number to link it. Shift-click to select a range.
| 1 | // Copyright (c) 2026 Reignance Studios. MIT License, see LICENSE. |
| 2 | |
| 3 | #include "GKLPlayerState.h" |
| 4 | |
| 5 | #include "AbilitySystemComponent.h" |
| 6 | #include "Abilities/GKLSampleAbilities.h" |
| 7 | #include "GKLAttributeSet.h" |
| 8 | #include "GKLGameplayTags.h" |
| 9 | #include "Misc/EngineVersionComparison.h" |
| 10 | |
| 11 | AGKLPlayerState::AGKLPlayerState(const FObjectInitializer& ObjectInitializer) |
| 12 | : Super(ObjectInitializer) |
| 13 | { |
| 14 | AbilitySystemComponent = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilitySystemComponent")); |
| 15 | AbilitySystemComponent->SetIsReplicated(true); |
| 16 | AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); |
| 17 | |
| 18 | // Created as a subobject of the ASC's owner, so the ASC finds and replicates it. |
| 19 | AttributeSet = CreateDefaultSubobject<UGKLAttributeSet>(TEXT("AttributeSet")); |
| 20 | |
| 21 | #if UE_VERSION_NEWER_THAN(5, 4, 99) |
| 22 | SetNetUpdateFrequency(100.f); |
| 23 | #else |
| 24 | NetUpdateFrequency = 100.f; |
| 25 | #endif |
| 26 | |
| 27 | StartupAbilities = { |
| 28 | UGKLAbility_Jump::StaticClass(), |
| 29 | UGKLAbility_Heal::StaticClass(), |
| 30 | UGKLAbility_Strike::StaticClass(), |
| 31 | UGKLAbility_Regen::StaticClass(), |
| 32 | UGKLAbility_Death::StaticClass() |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | UAbilitySystemComponent* AGKLPlayerState::GetAbilitySystemComponent() const |
| 37 | { |
| 38 | return AbilitySystemComponent; |
| 39 | } |
| 40 | |
| 41 | void AGKLPlayerState::BeginPlay() |
| 42 | { |
| 43 | Super::BeginPlay(); |
| 44 | |
| 45 | if (!HasAuthority() || bStartupAbilitiesGranted) |
| 46 | { |
| 47 | return; |
| 48 | } |
| 49 | for (const TSubclassOf<UGameplayAbility>& Ability : StartupAbilities) |
| 50 | { |
| 51 | if (Ability) |
| 52 | { |
| 53 | AbilitySystemComponent->GiveAbility(FGameplayAbilitySpec(Ability, 1, INDEX_NONE, this)); |
| 54 | } |
| 55 | } |
| 56 | bStartupAbilitiesGranted = true; |
| 57 | } |
| 58 | |
| 59 | void AGKLPlayerState::ResetForRespawn() |
| 60 | { |
| 61 | if (!HasAuthority()) |
| 62 | { |
| 63 | return; |
| 64 | } |
| 65 | GKLTags::RemoveReplicatedLooseTag(AbilitySystemComponent, GKLTags::State_Dead); |
| 66 | AttributeSet->ResetToMax(); |
| 67 | |
| 68 | // Death cancelled the passives; State.Dead is gone now, so they can start again. |
| 69 | FGameplayTagContainer Passives; |
| 70 | Passives.AddTag(GKLTags::Ability_Passive); |
| 71 | AbilitySystemComponent->TryActivateAbilitiesByTag(Passives); |
| 72 | } |