-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGiveAbilityWithInputTag.cpp
103 lines (84 loc) · 3.36 KB
/
GiveAbilityWithInputTag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Fill out your copyright notice in the Description page of Project Settings.
#include "GiveAbilityActor.h"
#include "AbilitySystem/LyraAbilitySystemComponent.h"
#include "AbilitySystemGlobals.h"
#include "Kismet/KismetStringLibrary.h"
// Sets default values
AGiveAbilityActor::AGiveAbilityActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
TriggerCapsule->InitCapsuleSize(100.f, 100.0f);;
TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
TriggerCapsule->SetupAttachment(RootComponent);
TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &AGiveAbilityActor::OnOverlapBegin);
TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &AGiveAbilityActor::OnOverlapEnd);
}
void AGiveAbilityActor::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (bEnableDebugMessages)
{
UE_LOG(LogTemp, Warning, TEXT("GiveAbilityActor, Overlapped"));
}
UAbilitySystemComponent* LyraASC = UAbilitySystemGlobals::GetAbilitySystemComponentFromActor(OtherActor);
check(LyraASC);
TArray<FGameplayAbilitySpecHandle> AbilityHandles;
LyraASC->GetAllAbilities(AbilityHandles);
TArray<FString> AbilityHandleStrings;
if(AbilityHandles.Num() > 0)
{
for (int i = 0; i < AbilityHandles.Num(); i++)
{
FString AbilityString = LyraASC->FindAbilitySpecFromHandle(AbilityHandles[i])->Ability->GetName();
AbilityHandleStrings.Add(AbilityString);
if (AbilityString.MatchesWildcard("*_*") && bEnableDebugMessages)
{
UE_LOG(LogTemp, Warning, TEXT("GiveAbilityActor, User already has ability, %s"), *AbilityString);
}
}
}
if (!LyraASC->IsOwnerActorAuthoritative())
{
// Must be authoritative to give or take ability sets.
if (bEnableDebugMessages)
{
UE_LOG(LogTemp, Warning, TEXT("GiveAbilityActor, Musc be Authoritive"));
}
return;
}
for (int32 AbilityIndex = 0; AbilityIndex < GrantedGameplayAbilities.Num(); ++AbilityIndex)
{
const FGiveAbilityActor_GameplayAbility& AbilityToGrant = GrantedGameplayAbilities[AbilityIndex];
if (!AbilityToGrant.bAllowStacking && AbilityHandleStrings.Contains("Default__" + AbilityToGrant.Ability->GetName()))
{
if (bEnableDebugMessages)
{
UE_LOG(LogTemp, Warning, TEXT("GiveAbilityActor, User already has ability, and stacking disabled"));
}
continue;
}
if (!IsValid(AbilityToGrant.Ability))
{
if (bEnableDebugMessages)
{
UE_LOG(LogTemp, Error, TEXT("GrantedGameplayAbilities[%d] on ability set [%s] is not valid."), AbilityIndex, *GetNameSafe(this));
}
continue;
}
ULyraGameplayAbility* AbilityCDO = AbilityToGrant.Ability->GetDefaultObject<ULyraGameplayAbility>();
FGameplayAbilitySpec AbilitySpec(AbilityCDO, AbilityToGrant.AbilityLevel);
AbilitySpec.SourceObject = OtherActor;
AbilitySpec.DynamicAbilityTags.AddTag(AbilityToGrant.InputTag);
LyraASC->GiveAbility(AbilitySpec);
}
}
void AGiveAbilityActor::OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
if (bEnableDebugMessages)
{
UE_LOG(LogTemp, Warning, TEXT("GiveAbilityActor, Overlapped Ended"));
}
}